title: Abnormal closure of WebSocket does not return ReceiveAsync
body: <p>I have a thread on the server for processing data received from a client over a WebSocket connection:</p>
  
  <pre><code>    public HttpResponseMessage Get()
      {
          if (HttpContext.Current.IsWebSocketRequest)
          {
              HttpContext.Current.AcceptWebSocketRequest(ProcessWebSocket);
          }
  
          return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
      }
  
      private async Task ProcessWebSocket(AspNetWebSocketContext context)
      {
          WebSocket webSocket = context.WebSocket;    //Gets the current WebSocket object. 
          const int maxMessageSize = 1024;
          byte[] receivedDataBuffer = new byte[maxMessageSize];
          logger.Debug("WSC-New WebSocket connection");
          while (webSocket.State == WebSocketState.Open)
          {
              var timeOut = new CancellationTokenSource(70000).Token;
              WebSocketReceiveResult webSocketReceiveResult = await webSocket.ReceiveAsync(new ArraySegment&lt;byte&gt;(receivedDataBuffer), timeOut);
              //If input frame is cancelation frame, send close command. 
              if (webSocketReceiveResult.MessageType == WebSocketMessageType.Close)
              {
                  await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
                  logger.Debug("WSC-Close WebSocket connection frame received");
              }
              else
              {
                  //Process received data....
              }
          }
          logger.Debug("WSC-WebSocket closed, WebSocket state: {0}", webSocket.State.ToString());
      }
  </code></pre>
  
  <p>As long as the other side closes the WebSocket connection correctly I get the logger message at the end that says the websocket was closed, which means this thread terminated correctly. If the client goes to sleep or the program stops abruptly then a normal WebSocket closure does not occur. In that case webSocket.ReceiveAsync never returns. I am worried that I might have a serious memory leak if enough of these threads are started but never terminated. The timeout cancellation token is supposed to make it return after 70 seconds but it does not happen. If I try to access the webSocket object from another thread in this situation I get the following exception: Cannot access a disposed object. </p>
  
  <p>How can I get back from await webSocket.ReceiveAsync(new ArraySegment(receivedDataBuffer), timeOut); to close this thread properly?</p>