title: Websocket connection keeps prematurely closing the connection
body: <p>So, I am trying to build a C application which functions as a Websockets Server.There are many related questions on here, but none of them seem to be able to help with the problem. The program is able to initially establish a connection, complete the handshake for the websocket successfully, however, cannot seem to be able to keep the connection open. On the client side, I get the error </p>
  
  <blockquote>
    <p>WebSocket.js:7605 Uncaught Error: Attempt to send message on unopened or closed WebSocket
        at _8b2.window.WebSocket._8d2.send (WebSocket.js:7605)
        at (index):34</p>
  </blockquote>
  
  <p>whenever I try to use the send() function on the websocket connection on the client side. And also it gives me the error </p>
  
  <blockquote>
    <p>WebSocket connection to 'ws://127.0.0.1:5959/?.kl=Y' failed: WebSocket is closed before the connection is established.</p>
  </blockquote>
  
  <p>Here is the source code of the server:</p>
  
  <pre><code> int new_socket;
  int activity;
  int i;
  int sd;
  int max_sd;
  int bytes_read;
  fd_set readfds;
  int master_socket;
  noPollConn* new_conn;
  /*Create no poll context*/
  noPollCtx* ctx = nopoll_ctx_new();
  noPollConn* conn;
  char buffer[3000];
  /*Create a connection listener at 127.0.0.1 (loopback) on port 5959*/
  noPollConn * listener = nopoll_listener_new(ctx, "127.0.0.1","5959");
  /*Get the socket of the lister*/
  master_socket = nopoll_conn_socket(listener);
  if(!nopoll_conn_is_ok(listener)) {
      perror("There was an error creating the listener!\n");
      exit(EXIT_FAILURE);
  }
  puts("Waiting for connections ...");
  
  while(TRUE){
      printf("Start of the while loop\n");
      FD_ZERO(&amp;readfds);
      FD_SET(master_socket, &amp;readfds);
      max_sd = master_socket;
      printf("The number of connections is %i\n",cons);
  
      for (i = 0 ; i &lt; cons ; i++) {
          sd = nopoll_conn_socket((noPollConn*)vector_get(clients,i));
          if(sd &gt; 0)
              FD_SET(sd,&amp;readfds);
  
          if(sd &gt; max_sd)
              max_sd = sd;
      }
      printf("The max fd is %i\n",max_sd);
      activity = select(max_sd + 1 , &amp;readfds , NULL , NULL , NULL);
  
      if ((activity &lt; 0) &amp;&amp; (errno!=EINTR)) {
          puts("select error");
          exit(EXIT_FAILURE);
      }
  
      if (FD_ISSET(master_socket, &amp;readfds)){
          new_conn =  nopoll_conn_accept(ctx,listener);
          puts("Waiting for the connection to be ready");
          nopoll_conn_is_ready(conn);
          /*Vector is actually a doubly linked list*/
          vector_push(&amp;clients,new_conn);
          cons++;
      }
      /*TODO: Implement disconnect*/
      for (i = 0; i &lt; cons; i++){
          printf("Checking on user %i\n",i);
          conn = (noPollConn*)vector_get(clients,i);
          sd = nopoll_conn_socket(conn);
          if (FD_ISSET(sd, &amp;readfds)){
              printf("Receiving info from socket no. %d...\n",sd);
              bytes_read = recv(sd,buffer,4000,MSG_DONTWAIT);
              buffer[bytes_read] = '\0';
              printf("Received the msg --&gt; %s\n",buffer);
  
          }
      }
      memset(buffer,0,3000);
  }
  </code></pre>
  
  <p>Just a warning though, this code does go in an infinite loop right now as I have not implemented disconnection from the client on the server side.</p>
  
  <pre><code>For the client
  &lt;pre&gt;&lt;code&gt;
              var connection = new WebSocket('ws://127.0.0.1:5959');
              connection.onopen = function(){
                  connection.send("TEST");
                  alert("Connected");
              }
              connection.onerror = function(error){
                 console.log('Error detected: ' + error);
              }
              connection.onmessage = function (event) {
                  alert(event.data);
              }
  
              connection.close();
  &lt;/code&gt;&lt;/pre&gt;
  </code></pre>
  
  <p>Perhaps I am missing something very important? I have gone through many tutorials, and I can't seem to figure out what the issue is. Any help is greatly appreciated!
  Calling <code>.send()</code> in the <code>.onopen()</code> function results in the error:</p>
  
  <blockquote>
    <p>WebSocket connection to 'ws://127.0.0.1:5959/?.kl=Y' failed: WebSocket is closed before the connection is established.
    <strong>UPDATE</strong><br>
    The error is on the server side. It might be related to me handling the connection as though it were a socket in some regards in the C code. </p>
  </blockquote>