title: Sending + Receiving Data on same socket in C#
body: <p>I'm trying to use a socket (System.Net.Socket, even tried TcpListener/Client/Etc.) to be able to listen for data, while it's waiting to or already sending data. </p>
  
  <p>I've done the following:</p>
  
  <pre><code>    public byte[] bytesIn;
      public byte[] bytesOut;
      public Socket transmitter;
      public Socket receiver;
  
      public Comlink(String ipAddress, int portNum) 
      {
  
          try
          {
              IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ipAddress), 11000);
  
              transmitter = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              transmitter.Listen(10);
  
              receiver = transmitter.Accept();
              receiver.Receive(bytesIn);
  
              try
              {
                  transmitter.Connect(remoteEP);
                  this.bytesOut = Encoding.ASCII.GetBytes("&lt;SYNCUP&gt;");
                  int bytesSent = transmitter.Send(this.bytesOut);
                  this.connected = true;
              }
          }  // (Omitted my exception handling.)
      }
  </code></pre>
  
  <p>The Receive() function:</p>
  
  <pre><code>this.bytesIn = new byte[1024];
  Socket receiver = transmitter.Accept();
  int bytesReceived = receiver.Receive(this.bytesIn);
  </code></pre>
  
  <p>Code that binds it:</p>
  
  <pre><code>while(comlink.connected)
  {
      comlink.Receive();
      handlePacket(comlink.bytesIn);
  }
  </code></pre>
  
  <p>I'm just not entirely sure if I'm using the right process to receive data, as that's always been the complaint from .NET. I need data to be handled as it comes in.</p>
  
  <p>Something like a two-way street: Data can freely come in (without the program having to move back to Receive() and wait for the data), it gets handled. Data can move freely from the client and to the server, as well. I know TCP sockets are bidirectional, but I need to improve this class so that I can enjoy the full benefits of having a free, open road.</p>