title: C# TCP client sending but not receiving messages
body: <p>I inherited some code for a TCP client.  It sends a message, then when complete waits for a confirmation message.  Currently it sends the message, but when it waits for the reply message it never receives it.  The <code>SendMessage()</code> method is what is called externally.</p>
  
  <p>I set breakpoints and can see it hits the <code>Receive()</code> method, but never the <code>ReceiveCallback()</code></p>
  
  <p>Any help or nudge in the right direction is appreciated.</p>
  
  <pre><code>public class SocketService
  {
      private ManualResetEvent connectDone = new ManualResetEvent(false);
      private ManualResetEvent sendDone = new ManualResetEvent(false);
      private ManualResetEvent receiveDone = new ManualResetEvent(false);
  
      string ipAddress;
      private int portNum;
      string returnMessage;
      // private string hostName = Dns.GetHostName ();
  
      public SocketService(string ipAddress, int portNum)
      {
          this.ipAddress = ipAddress;
          this.portNum = portNum;
      }
  
      public event Logging OnLogging;
  
      private void WriteLogMessage(string message)
      {
          if (this.OnLogging != null)
              this.OnLogging(message);
      }
  
      //char endOfLineSeparator = (char) 0x1C;
      //char cr = (char) 0x0D;
      //char vt = (char) 0x0B;
  
      public string SendMessage(string message)
      {
          IPAddress ip = IPAddress.Parse(this.ipAddress);
          IPEndPoint remoteEP = new IPEndPoint(ip, portNum);
  
          connectDone.Reset();
  
          Socket client = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);
  
          client.BeginConnect(remoteEP,
                              new AsyncCallback(ConnectCallback),
                              client);
  
          connectDone.WaitOne();
  
          //sendDone.Reset();
  
          WriteLogMessage("send message");
  
          Send(client, message);
          sendDone.WaitOne();
  
          //receiveDone.Reset();
  
          WriteLogMessage("receive message");
  
          Receive(client);
          receiveDone.WaitOne();
  
          client.Shutdown(SocketShutdown.Both);
          client.Close();
  
          return this.returnMessage;
      }
  
      private void Send(Socket client, String data)
      {
          byte[] byteData = Encoding.ASCII.GetBytes(data);
  
          client.BeginSend(byteData,
                          0,
                          byteData.Length,
                          0,
                          new AsyncCallback(SendCallback),
                          client);
      }
  
      private void SendCallback(IAsyncResult ar)
      {
          Socket client = (Socket)ar.AsyncState;
  
          int bytesSent = client.EndSend(ar);
  
          sendDone.Set();
      }
  
      private void ConnectCallback(IAsyncResult ar)
      {
          Socket client = (Socket)ar.AsyncState;
  
          client.EndConnect(ar);
  
          connectDone.Set();
      }
  
      private void Receive(Socket client)
      {
          StateObject state = new StateObject(client);
  
          client.BeginReceive(state.Buffer,
                              0,
                              StateObject.BufferSize,
                              0,
                              new AsyncCallback(ReceiveCallback),
                              state);
      }
  
      private void ReceiveCallback(IAsyncResult ar)
      {
          StateObject state = (StateObject)ar.AsyncState;
          Socket client = state.WorkSocket;
  
          int bytesRead = client.EndReceive(ar);
  
          if (bytesRead &gt; 0)
          {
              state.StoredContent.Append(Encoding.ASCII.GetString(state.Buffer, 0, bytesRead));
  
              client.BeginReceive(state.Buffer,
                                  0,
                                  StateObject.BufferSize,
                                  0,
                                  new AsyncCallback(ReceiveCallback),
                                  state);
  
              WriteLogMessage(state.StoredContent.ToString());
          }
          else
          {
              this.returnMessage = state.StoredContent.ToString();
  
              receiveDone.Set();
          }
      }
  }
  </code></pre>