title: NetworkStream gets System.IO.IOException: Unable to write data to the transport connection
body: <p>I am using a NetworkStream to keep an open TCP/IP connection that messages can be sent across. I receive a message, process it, and then return an ACK. I am working with a site where occasionally I receive the message, but when I go to send the ACK, I get an IOException. Sometimes this lasts for only one or two messages (I can receive the next message), and other times it continues until the service is stopped and restarted.</p>
  
  <p>Below is the code for my NetworkStream without any of the processing:</p>
  
  <pre><code>using (NetworkStream stream = client.GetStream())
  {
     stream.ReadTimeout = ReadTimeout;
  ...
     if (stream.CanRead &amp;&amp; stream.DataAvailable)
     bytesRead = stream.Read(receivedBytes, 0, BufferSize);
  ...
     stream.Write(ack, 0, ack.Length);
  }
  </code></pre>
  
  <p>Note that the code loops between reading new messages and sending the ACK. </p>
  
  <p>When I call <code>stream.Write</code>, I will sometimes get the following exception:</p>
  
  <pre><code>System.IO.IOException: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. ---&gt; System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
     at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
     --- End of inner exception stack trace ---
     at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
     at Framework.Communication.Model.Server.AcceptMessage(IAsyncResult ar)
  </code></pre>
  
  <p>I have looked this message up and found several <a href="https://stackoverflow.com/questions/14304658/c-sharp-an-established-connection-was-aborted-by-the-software-in-your-host-machi">sources</a> for it for several different communication methods. It sounds like this is a very generic exception that does not really tell what is happening.</p>
  
  <p>Does anyone know what would cause this, or where I could start looking to narrow down the solution?</p>