title: Loop until TcpClient response fully read
body: <p>I have written a simple TCP client and server. The problem lies with the client.</p>
  
  <p>I'm having some trouble reading the entire response from the server. I must let the thread sleep to allow all the data be sent.</p>
  
  <p>I've tried a few times to convert this code into a loop that runs until the server is finished sending data.</p>
  
  <pre><code>// Init &amp; connect to client
  TcpClient client = new TcpClient();
  Console.WriteLine("Connecting.....");
  client.Connect("192.168.1.160", 9988);
  
  // Stream string to server
  input += "\n";
  Stream stm = client.GetStream();
  ASCIIEncoding asen = new ASCIIEncoding();
  byte[] ba = asen.GetBytes(input);
  stm.Write(ba, 0, ba.Length);
  
  // Read response from server.
  byte[] buffer = new byte[1024];
  
  System.Threading.Thread.Sleep(1000); // Huh, why do I need to wait?
  
  int bytesRead = stm.Read(buffer, 0, buffer.Length);
  response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
  Console.WriteLine("Response String: "+response);
  
  client.Close();
  </code></pre>