title: Getting IOException with NetworkStream.BeginRead()
body: <p>When running this code:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>private async void StartChat(Object obj)
  {
      TcpClient me = (TcpClient)obj;
      UpdateChatBox("Attempting read from server.");
      myBuffer = new byte[BUFFER_SIZE];
  
      while (true)
      {
          var myStream = me.GetStream();
          myStream.BeginRead(myBuffer, 0, BUFFER_SIZE, new AsyncCallback(UpdateChatBoxAsync), myStream);
  
          if (messageToSend)
          {
              await myStream.WriteAsync(myMessage, 0, myMessage.Length);
          }                
      }
  }
  </code></pre>
  
  <p>I am receiving the following IO Exception from <code>BeginRead</code>:</p>
  
  <blockquote>
    <p>Unable to read data from the transport connection: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.</p>
  </blockquote>
  
  <p>Here is the callback method for BeginRead:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void UpdateChatBoxAsync(IAsyncResult result)
  {
      var stream = result.AsyncState as NetworkStream;
      int bytesRead = stream.EndRead(result);
  
      if (bytesRead &gt; 0)
      {
          String newMessage = NetworkUtils.GetString(myBuffer);
          UpdateChatBox(newMessage);
      }
  }
  </code></pre>
  
  <p>Can someone please shed some light as to the reason this exception is being raised? I tried recreating the buffer each time at the start of the while loop, but while that worked in not raising the exception, I would not receive messages from the server.</p>
  
  <p>I also attempted to reset myBuffer to an empty array at the end of UpdateChatBoxAsync, this did not work either.</p>
  
  <p>Any help would be appreciated.</p>