title: A websocket's ReceiveAsync method does not await the entire message
body: <p>I am receiving JSON through a websocket. At least: I am partially. Using an online websocket service I receive the full JSON response (all the HTML markup is ignored). When I look at the JSON that I receive in my console I can see the HTML markup (viewing it with the HTML viewer during debugging removes the HTML) but it ends abruptly (incomplete data).</p>
  
  <p>My buffer has plenty of space and I am using async-await to (supposedly) wait for the entire response to come in before continuing. </p>
  
  <pre><code>private async Task Receive()
  {
    var buffer = new byte[4096 * 20];
  
    while (_socket.State == WebSocketState.Open)
    {
        var response = await _socket.ReceiveAsync(new ArraySegment&lt;byte&gt;(buffer), CancellationToken.None);
  
        if (response.MessageType == WebSocketMessageType.Close)
        {
            await
                _socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Close response received",
                    CancellationToken.None);
        }
        else
        {
            var result = Encoding.UTF8.GetString(buffer);
            var a = buffer[1000];
            var b = buffer[10000];
            var c = buffer[50000];
            var d = buffer[81000];
            Console.WriteLine(result);
            var responseObject = JsonConvert.DeserializeObject&lt;Response&gt;(result, _requestParameters.ResponseDataType);
  
            OnSocketReceive.Invoke(this, new SocketEventArgs {Response = responseObject });
            buffer = new byte[4096 * 20];
        }
    }
  }   
  </code></pre>
  
  <p>Things to note: The buffer is perfectly big enough and <code>b</code>, <code>c</code> and <code>d</code> are never filled. I should also note that this only happens for the <code>1-questions-newest-tag-java</code> request, <code>155-questions-active</code> works perfectly fine.</p>
  
  <p>After doing some digging I have found that <code>response.CloseStatus</code> and <code>response.CloseStatusDescription</code> are always <code>null</code>, <code>response.Count</code> is always <code>1396</code> (copy-pasting the result in Word does show that there are always 1396 characters) and <code>response.EndOfMessage</code> is <code>false</code>.</p>
  
  <p>Digging through <a href="http://referencesource.microsoft.com/#System/net/System/Net/WebSockets/ClientWebSocket.cs#6fd04c76fe32d754" rel="noreferrer">some source code</a> I have found that the <a href="http://referencesource.microsoft.com/#System/net/System/Net/WebSockets/WebSocketHelpers.cs#285b8b64a4da6851" rel="noreferrer"><code>DefaultReceiveBufferSize</code></a> is <code>16 * 1024</code> (big enough) and the <code>WebSocketGetDefaultKeepAliveInterval()</code> refers to <a href="http://referencesource.microsoft.com/#System/net/System/Net/WebSockets/WebSocketProtocolComponent.cs#3039008acf8baff7" rel="noreferrer">an external implementation</a> (but the debugger shows <code>00:00:30</code>).</p>
  
  <p>It is not a matter of timeout since the debugger halts at the same moment the online service receives its response.</p>
  
  <p>Why is my method continuing to execute when the socket has not yet received all data? </p>