title: C# TCP server and client with cross-communication requirement
body: <p>I need some feedback with some programming logic. </p>
  
  <p>I'm developing a TCP Server using C#, which will also act as a TCP client. Basically, a client device (A) contacts my TCP Server and sends information. Based on that the TCP server may (or may not) talk to another server (Z) over TCP, send some data to it and <em>sometimes</em> get a response which it passes back to device A. So far so good, this works quite well. However, sometimes Z creates a new connection to my C# TCP server, and wants to send some data to device A. </p>
  
  <p>This is what I am struggling with. I can get the data from Z, read it and see the destination IP address in the text that is read. But how do I send that to device A, which is still connected via a TCPListener on it's own thread?</p>
  
  <p>I could use static variables and the "lock" mechanism to have the thread look for something to send to the device, but what happens if I add device B &amp; C as well?  </p>
  
  <p>I basically need some way of, based on the data sent from Z on a new connection, determining which TCPListener on which thread the data should be sent to the devices.</p>
  
  <p>It's doing my head in so any links you may have or rough logic (doesn't have to be code) would greatly help.</p>
  
  <p>And yes, I've been searching Google for a while, but all the examples I have found are simpler than what I need to do.</p>
  
  <p>Thanks in advance!</p>