title: Tcp/Ip Socket connections in .NET
body: <p>For my current project, I need to request XML data over a tcp/ip socket connection.  For this, I am using the TcpClient class:</p>
  
  <pre><code>Dim client As New TcpClient()
  client.Connect(server, port)
  
  Dim stream As NetworkStream = client.GetStream()
  stream.Write(request)
  stream.Read(buffer, 0, buffer.length)
  
  // Output buffer and return results...
  </code></pre>
  
  <p>Now this works fine and dandy for small responses.  However, when I start receiving larger blocks of data, it appears that the data gets pushed over the socket connection in bursts.  When this happens, the stream.Read call only reads the first burst, and thus I miss out on the rest of the response.  </p>
  
  <p>What's the best way to handle this issue?  Initially I tried to just loop until I had a valid XML document, but I found that in between stream.Read calls the underlying stream would sometimes get shut down and I would miss out on the last portion of the data.</p>