title: Closing socket in client crashes nodejs server
body: <p>On C client I do:</p>
  
  <pre><code>socket()
  connect() on port 6969
  send()
  //I have seen that I didn't call recv so nodejs try me to send data but My program was gone
  and finally closesocket()
  </code></pre>
  
  <p>On nodejs server I receive the message so the connection is established:</p>
  
  <pre><code>const port = 6969;
  var net = require('net');
  var server = net.createServer(function(connection) {
      console.log('client connected');
      connection.on('close', function() {
          console.log('conn closed');
      });
      connection.on('end', function() {
          console.log('conn ended');// that is not called
      });
      connection.on("error", function(err) {
          console.log("Caught flash policy server socket error: ");
          console.log(err.stack);
      });
      connection.on('data', function(data) {
          data = data.toString();
          console.log('client sended the folowing string:' + data);
          connection.write("Response");
          console.log('Sended response to client');
      });
  });
  server.listen(port, function() {
      console.log('server is listening');
  });
  </code></pre>
  
  <p>This is the result of my terminal:</p>
  
  <pre><code>server is listening
  client connected
  client sended the folowing string:err404
  Sended response to client
  Caught flash policy server socket error:
  Error: read ECONNRESET
      at exports._errnoException (util.js:1018:11)
      at TCP.onread (net.js:568:26)
  conn closed
  </code></pre>
  
  <p>So I have read <a href="https://stackoverflow.com/questions/17245881/node-js-econnreset">Node js ECONNRESET</a> already but I don't understand if this is normal why my nodejs server crash?</p>
  
  <p>Edit: I found this snippet:</p>
  
  <pre><code>connection.on("error", function(err) {
      console.log("Caught flash policy server socket error: ");
      console.log(err.stack);
  });
  </code></pre>
  
  <p>This code on client side will produce the same error:</p>
  
  <pre><code>#ifdef WIN32
  Sleep(5000);
  int iResult = shutdown(sock, SD_BOTH);
  printf("shutdown is called\n");
  Sleep(5000);
  #elif defined (linux)
  sleep(5);
  int iResult = shutdown(sock, SHUT_RDWR);
  printf("shutdown is called\n");
  sleep(5);
  #endif // WIN32
      if (iResult == SOCKET_ERROR) {closesocket(sock);printf("SOCKET_ERROR");}
      printf("iResult=%d",iResult);
  </code></pre>
  
  <p>Edit:
  Now I catch either close event and end event: but the same error is still throwed.</p>
  
  <p>Edit: I've Updated my code.
  And I found where is the problem:NodeJs is trying to send me data but I've already called <code>shutdown()</code>.</p>