title: Handling a "System.Net.Websocket-Exception : The remote party close the connection without completing the close handshake."?
body: <p>I can able to chat if I continuously chat. If I leave for a 2-3 mins WebSocket connection close without handshake error message I am getting I don't know how to handle.</p>
  
  <p>Can anyone please help how to handle the exception.i have attached the crash log and here is my code.</p>
  
  <p><a href="https://i.stack.imgur.com/Hxxx3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hxxx3.png?s=256" alt="Crash Report"></a>
    <br/></p>
  
  <p><em>click thumbnail to see the full error</em></p>
  
  <p><br/></p>
  
  <pre><code>public async void ConnectToServerAsync()
  {
      await client.ConnectAsync(new Uri("ws://api.testurl.com/ws"), CancellationToken.None);
  
  
      UpdateClientState();
  
      await Task.Factory.StartNew(async () =&gt;
      {
          Refresh();
          while (client.State == WebSocketState.Open)
          {
              WebSocketReceiveResult result;
              var message = new ArraySegment&lt;byte&gt;(new byte[4096]);
              var allBytes = new List&lt;byte&gt;();
              do
              {
                  result = await client.ReceiveAsync(message, CancellationToken.None);   
  
                  for (int i = 0; i &lt; result.Count; i++)
                  {
                      allBytes.Add(message.Array[i]);
                  }
  
              } while (!result.EndOfMessage);
              try
              {
                  Debug.WriteLine("End of Message2 :" + result.EndOfMessage);
                  Debug.WriteLine("All Bytes :" + allBytes.ToString());
                  // Optional step to convert to a string (UTF-8 encoding).
                  var serialisedMessae = Encoding.UTF8.GetString(allBytes.ToArray(), 0, allBytes.Count);
                  var msg = JsonConvert.DeserializeObject&lt;List&lt;ChatModel&gt;&gt;(serialisedMessae);
                  Debug.WriteLine("Message Count :" + msg.Count);
                  if (Chatlist.Count != msg.Count)
                  {
                      Chatlist.Clear();
                      foreach (var item in msg)
                      {
                          var details = new ChatModel()
                          {
                              Message = item.Message,
                              MessageFrom = item.MessageFrom,
                              MessageTo = item.MessageTo,
                              RoomId = item.RoomId,
                              _id = item._id,
                              TimeStamp = item.TimeStamp.ToLocalTime()
                          };
                          Chatlist.Insert(0, details);
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"Invalide message format. {ex.Message}");
              }
          }
      }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
  
      void UpdateClientState()
      {
          OnPropertyChanged(nameof(IsConnected));
          sendMessageCommand.ChangeCanExecute();
          Console.WriteLine($"Websocket state {client.State}");
      }
  }
  </code></pre>