title: System.ObjectDisposedException throwed on WebSocket communication
body: <p>I'm trying to communicate between a web browser client and an ASP.NET server using WebSockets.</p>
  
  <p>I make a set of requests, of different sizes and with some seconds of elapsed time between each of them. The three first ones pass correctly, but a precise one, with nothing in particular from the other, close the WebSocket connection, throw an exception on server side.</p>
  
  <p>The error message and stack trace of this exception look like this :</p>
  
  <blockquote>
    <p>FATAL ERROR: Cannot access a disposed object.<br>
    Object name: 'System.Web.WebSockets.AspNetWebSocket'.<br>
      at System.Web.WebSockets.AspNetWebSocket.ThrowIfDisposed()<br>
      at System.Web.WebSockets.AspNetWebSocket.SendAsyncImpl(ArraySegment 1   buffer, WebSocketMessageType messageType, Boolean endOfMessage, CancellationToken cancellationToken, Boolean performValidation)<br>
      at System.Web.WebSockets.AspNetWebSocket.SendAsync(ArraySegment 1 buffer, WebSocketMessageType messageType, Boolean endOfMessage, CancellationToken cancellationToken)<br>
      at [my code path here...]</p>
  </blockquote>
  
  <p>It may be a threading problem, because I'm using <code>async</code> methods everywhere from functions that communicate with websockets.</p>
  
  <p>I know the exception is throwed from this code (at <code>socket.SendAsync</code>):</p>
  
  <pre><code>public class SocketTranslater
  {
  
      private WebSocket socket;
      private JavaScriptSerializer serializer;
  
      // [...]
  
      private ArraySegment&lt;byte&gt; Encode(Object data)
      {
          string json = serializer.Serialize(data);
          return (new ArraySegment&lt;byte&gt;(Encoding.UTF8.GetBytes(json)));
      }
  
  
      public async Task Send(Object packet)
      {
          ArraySegment&lt;byte&gt; encoded = this.Encode(packet);
  
          await socket.SendAsync(encoded, WebSocketMessageType.Text, true, CancellationToken.None);
      }
  }
  </code></pre>
  
  <p>The socket is created from another class :</p>
  
  <pre><code>public class EventSender : IHttpHandler
  {
      private static List&lt;SocketTranslater&gt; socketTranslaters = new List&lt;SocketTranslater&gt;();
  
      public void ProcessRequest(HttpContext context)
      {
          this.id = context.Request.UserHostName + ":" + context.Request.UserAgent;
          if (context.IsWebSocketRequest)
          {
              context.AcceptWebSocketRequest(ProcessSocket);
          }        
      }
  
      private async Task ManageSocket(AspNetWebSocketContext context)
      {
          WebSocket socket = context.WebSocket;
          SocketTranslater translater = new SocketTranslater(socket);
          translaters.add(translater);
  
          while (true)
          {
              // Manage messages, using the other class to send responses.
              translater.Send(/* Any struct here */);
          }
      }
  </code></pre>
  
  <p>Unhopefully, my project is too big to put all the code here.</p>
  
  <p>Any idea of error source, or additional information that you would require ?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After some more tests, I don't have this exception from time to time. I always do the same thing, but the server seems to have a random comportment.</p>
  
  <p>That makes my problem even weirder...</p>