title: Dis connect websocket in c# after many minutes
body: <p>I made a desktop program with c# an use 
  web socket ((use : <a href="https://www.nuget.org/packages/WebSocketSharp/1.0.3-rc11" rel="nofollow noreferrer">https://www.nuget.org/packages/WebSocketSharp/1.0.3-rc11</a>)) into
  and server side script  is PHP web socket ((use : <a href="http://socketo.me/" rel="nofollow noreferrer">http://socketo.me/</a>))
  but I encountered some errors.</p>
  
  <p>1-When I call the Send method, the readyState switches to 'close' and I have to call Connect method before Send .</p>
  
  <p>2-When The program stays idle for a while connection is close </p>
  
  <p>Please help me 2 errors.
  Thank you very much</p>
  
  <p>my code c#:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.Windows.Forms;
  using System.Threading.Tasks;
  using System.Threading;
  using Newtonsoft.Json;
  using System.IO;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using WebSocketSharp;
  using System.Globalization;
  
  namespace MyApp
  {
      public partial class Form1 : Form
      {
  
          public WebSocket ws = new WebSocket("ws://example.com:4563");
  
          private static readonly HttpClient client = new HttpClient();
          public int request_id;    
          public bool online;
  
          public async Task&lt;String&gt; login(String id, String password)
          {
              var values = new Dictionary&lt;string, string&gt;
              {
                  { "id",id },
                  { "password",password }
              };
              var content = new FormUrlEncodedContent(values);
              var response = await client.PostAsync("https://example.com/Login", content);
              var responseString = await response.Content.ReadAsStringAsync();
  
              return responseString;
          }
  
          public void websocket()
          {
              button1.Enabled = button4.Enabled = button5.Enabled = false;
  
              ws.OnOpen += (sende, E) =&gt;
              {
                  //conncect app
  
              };
  
              ws.OnClose += (sende, E) =&gt;
              {
  
              };
  
              ws.OnMessage += (sende, E) =&gt;
              {
                  dynamic response = JsonConvert.DeserializeObject(E.Data);
  
                  switch ((String)response.type)
                  {
                      case "push_for_app":
                          {
                              label10.Text = (String)response.response;
                              turn_count = (String)response.response;
                              if ((String)response.response == "0")
                              {
                                  button1.Enabled = button4.Enabled = button5.Enabled = false;
                              }
                              else if (online) button1.Enabled = true;
                          }
                          break;
  
  
                      case "request":
                          {
                              switch ((String)response.method)
                              {
                                  case "logout":
                                      {
                                          if ((String)response.response == "true")
                                          {
                                              groupBox1.Visible = false;
                                              groupBox4.Visible = false;
                                              groupBox1.Visible = false;
                                              groupBox4.Visible = false;
                                              groupBox2.Visible = true;
                                          }
                                      }
                                      break;
  
  
                                  case "get_turn_count":
                                      {
                                          label10.Text = (String)response.response;
                                          turn_count = (String)response.response;
                                          if ((String)response.response == "0")
                                          {
                                              button1.Enabled = button4.Enabled = button5.Enabled = false;
                                          }
                                          else if (online) button1.Enabled = true;
                                      }
                                      break;
  
                                  case "set_online":
                                      {
                                          if ((String)response.response == "is_online")
                                          {
                                              button2.Visible = true;
                                              online = true;
                                              {
                                                  var template = new
                                                  {
                                                      type = "request",
                                                      method = "get_turn_count"
                                                  };
  
                                                  String output = JsonConvert.SerializeObject(template);
                                                  ws.Connect();
                                                  ws.Send(output);
                                              }
                                          }
                                      }
                                      break;
  
                                  case "set_offline":
                                      {
                                          if ((String)response.response == "is_offline")
                                          {
                                              button8.Visible = true;
                                              button1.Enabled = false;
                                              online = false;
                                          }
                                      }
                                      break;
  
  
                                  default: break;
                              }
                          }
                          break;
  
                      default: break;
                  }
              };
  
              var List = new List&lt;KeyValuePair&lt;String, String&gt;&gt; {
                      new KeyValuePair&lt;String, String&gt;("api-key", "jshTRHNSDB54n7y4e5nhty"),
                      new KeyValuePair&lt;String, String&gt;("secret-key", secret_key),
                      new KeyValuePair&lt;String, String&gt;("type", "app")
                      };
              ws.CustomHeaders = List;
              ws.Connect();
  
          }
          //login
          private void button7_Click(object sender, EventArgs e)
          {
              Task&lt;String&gt; task = Task.Run(async () =&gt; await login(textBox1.Text, textBox3.Text));
              task.Wait();
              dynamic res = JsonConvert.DeserializeObject(task.Result);
  
              if (res.state == "true")
              {
                  secret_key = res.message;
                  set_secret(secret_key);
                  groupBox1.Visible = true;
                  groupBox4.Visible = true;
                  groupBox2.Visible = false;
                  button8.Visible = true;
                  //connect socket
                  websocket();
              }
              else { label11.Text = res.message; }
          }
          //start app
          public Form1()
          {
              InitializeComponent();
              CheckForIllegalCrossThreadCalls = false;
              this.ShowInTaskbar = false;
              this.StartPosition = FormStartPosition.Manual;
              this.Location = new Point(SystemInformation.VirtualScreen.Width - this.Width, SystemInformation.VirtualScreen.Height - this.Height);
              this.BringToFront();
              this.TopMost = true;
              this.Focus();
          }
  
          //set offline
          private void button2_Click(object sender, EventArgs e)
          {
              var template = new
              {
                  type = "request_from_pharmacy",
                  method = "set_offline"
              };
  
              String output = JsonConvert.SerializeObject(template);
  
              ws.Connect();
              ws.Send(output);
  
              button2.Visible = false;
              button8.Visible = true;
          }  
  
          //set online
          private void button8_Click(object sender, EventArgs e)
          {
              var template = new
              {
                  type = "request",
                  method = "set_online"
              };
  
              String output = JsonConvert.SerializeObject(template);
  
              ws.Connect();
              ws.Send(output);
  
              button8.Visible = false;
              button2.Visible = true;
          }
  
      }
  }
  </code></pre>