title: How does NetworkStream work in two directions?
body: <p>I've read an example of a Tcp Echo Server and some things are unclear to me.</p>
  
  <pre><code>TcpClient client = null;
  NetworkStream netStream = null;
  
  try {
    client = listener.AcceptTcpClient(); 
    netStream = client.GetStream();
  
    int totalBytesEchoed = 0;
    while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) &gt; 0) {
      netStream.Write(rcvBuffer, 0, bytesRcvd);
      totalBytesEchoed += bytesRcvd;
    }
  
    netStream.Close();
    client.Close();
  } catch {
    netStream.Close();
  }
  </code></pre>
  
  <p>When the server receives a packet (the while loop), he reads the data into rcvBuffer and writes it to the stream.</p>
  
  <p>What confuses me is the chronological order of messages in communication. Is the data which was written with netStream.Write() sent immediately to the client (who may even still be sending), or only after the data which is already written to the stream (by client) processed.</p>
  
  <p>The following question may even clarify the previous: If a client sends some data by writing to the stream, is that data moved to the message queue on the server side waiting to be read so the stream is actually "empty"? That would explain why the server can immediately write to stream - because the data which comes from the stream is actually buffered elsewhere...?</p>