title: What is the correct way to close a TCP connection
body: <p>I have a TcpClient object which sends some data to server, using its underlying NetworkStream.Write().
  Therefor, I have:</p>
  
  <pre><code>TcpClient server = new TcpClient(serverName, 50001);
  
  /* ... */
  
  NetworkStream stream = server.GetStream();
  </code></pre>
  
  <p>Now, when a button is pressed, the connection should close.
  What is the right way to close the connection? The MSDN docs say that closing the TcpClient (with .Close()) does not in fact close the socket, only the TcpClient resources (that's at least the way I understood the docs).</p>
  
  <p>So, would doing the next code correctly close the connection?</p>
  
  <pre><code>stream.Close();
  server.Close();
  </code></pre>
  
  <p>Is this enough, or should I first check (somehow) if the stream (or server) can be closed (in case the connection is half-open or something)...</p>
  
  <p>Even more, <code>NetworkStream.Close()</code> MSDN docs states that it releases resources (even sockets), so maybe closing the stream would be enough, taken that I prevent using the TcpClient after that point.</p>
  
  <p>What is the right approach?</p>