title: c# Detect when ClientWebSocket is disconnected (closed)
body: <p>After a long search i come to you to help me.</p>
  
  <p>I have a WPF application, use ClientWebSocket class (using System.Net.WebSockets) to connect to WebSocket server(NodeJS). My application is running backgroung windows application, it connects at first time, and then, in timer of 1 minute try to re-connect (if not already connected). But when i disable the connexion (Network connection), my application doesn't detect that, so evry 1 minute tells me that it's connected. I know there is something wrong in my code, and i don't know how implement an event handler to be informed if the connetion is losed (websocket closed).</p>
  
  <p>I note that when i connect to the server, i must create a session, then i receive a session_id, and now i'm connected to websocket server. </p>
  
  <p>Class1 is a libreary class that i use WebSockets class</p>
  
  <pre><code>class1
  {
      ClientWebSocket web_socket;
      bool connected = false;
      public async Task&lt;bool&gt; connect_socket()
      { 
          Object result_object;
          string session_id;
          try
          {
              if (connected == false)
              {
                  web_socket = new ClientWebSocket();
                  web_socket.Options.SetRequestHeader("headername", "headerValue");
                  await web_socket.ConnectAsync(new Uri(" ... uri....."), CancellationToken.None);
  
                  create_session();
  
                  ArraySegment&lt;byte&gt; byte_received = new ArraySegment&lt;byte&gt;(new byte[1024]);
                  WebSocketReceiveResult json_notify_result = await web_socket.ReceiveAsync(byte_received, CancellationToken.None);
                  string string_notify = Encoding.UTF8.GetString(byte_received.Array);
                  json_notify = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(string_notify);
  
                  if (json_notify.TryGetValue("result", out result_object))
                  {
                      var result = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, string&gt;&gt;((result_object).ToString());
                      if (result.TryGetValue("session_id", out session_id) &amp;&amp; (string)session_id != null)
                          Debug.WriteLine("session_id = " + session_id);
                  }
                   connected = true;
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
           return connected;
      }
  }
  </code></pre>
  
  <p>In my Class2 i use Class1</p>
  
  <pre><code>Class2
  {
      Class2()
      {
          InitializeComponent();
          timer = new System.Threading.Timer(timer_callback, null, 0, 60000);
      }
      public void timer_callback()
      {
          if (Class1.connected == false)
          {
              Class1.connect_socket().Wait();
          }      
      }
  }
  </code></pre>
  
  <p>For any suggestion, i'll be very grateful. 
  I am really stuck in this point</p>