title: ClientWebSocket make sure that message has been sent and connection is alive
body: <p>Background: External factors force me to implement a simple websocket client in C# and I would like to keep the number of third party dependencies low. So it would be nice to stick with System.Net.WebSockets if possible.</p>
  
  <p>Objective: Send a series of JSON objects as individual messages to a websocket server. Throw an exception if sending fails with any reasonable timeout. Throw an exception if the underlying TCP connection breaks between new objects becoming available.</p>
  
  <p>Sending code:</p>
  
  <pre><code>// ws is an instance of System.Net.WebSockets.ClientWebSocket
  ArraySegment&lt;byte&gt; json = new ArraySegment&lt;byte&gt; 
  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)));
  
  if (!ws.SendAsync(json, WebSocketMessageType.Binary, true, 
  CancellationToken.None).Wait(10000))
  {
      throw new Exception("Error: Websocket send timeout");
  }
  Console.Write("Sent")
  </code></pre>
  
  <p>My problem with this code is that when I see the client program printing "Sent", the server will not receive that message until several more invocations of SendAsync. My impression is that it is stuck in some kind of internal message buffer until the buffer spills over.</p>
  
  <p>Similarly, breaking the underlying TCP connection does not result in the next SendAsync throwing an exception until several messages later.</p>
  
  <p>Checking if websocket is alive without sending:
  The keepalive is set to the default value 30s. Yet I have never seen ws.State return anything but WebSocketState.Open, even minutes after disconnecting the Server if I don't have any messages to send in between.</p>
  
  <p>Am I fundamentally confused about how ClientWebSocket is supposed to be used, does it simply not work, or did I miss some detail in the documentation?</p>