I have a question about sieges that always baffled me: How do you take a castle by treachery? As the besieger how can you identify and contact possible traitors in the castle?

Good question.

According to Aeneas Tacticus, author of the 4th century BCE best-seller How to Survive under Siege, key targets for treachery are social minorities, exiles, the families of hostages, and foreign mercenaries.

This is doubly true for any of them who are working as gatekeepers or guards, because those are the people who are physically closest to the enemy and could be communicated with more easily. 

But in terms of how you contact them, you get close to the walls and shout up to people, or you send in written letters, or you set up a parlay and use the opportunity to have your envoy talk to people face-to-face, or you send in a spy, etc.