title: Nodejs Error: This socket has been ended by the other party
body: <p>I have setup a simple server and client, however whenever I close the client, it seems not possible to reconnect. Here's my client:</p>
  
  <pre><code>    const net = require('net');
      var client = net.connect({port: 8080, host: '127.0.0.1'});
      var response = '';
      // events
      client.on('data', function(chunk) { response += chunk });
      client.on('end', function() { 
          console.log(response);
          client.end() 
       });
      // main execution
      client.write('test');
  </code></pre>
  
  <p>And here's my server:</p>
  
  <pre><code>    const net = require('net');
      var server = net.createServer();
      server.listen(8080, '127.0.0.1');
      server.on('connection', function(sock) {
          sock.on('data', function(chunk) {
              sock.write('test received');
              sock.end();
          });
      });
  </code></pre>
  
  <p>This is just outline code representing my issue. When I execute my client the first time, everything works correctly. However, when I execute it again, the server outputs the error mentioned in the title and crashes. The same happens if I remove 'client.end()' and instead Ctrl+C out of the client program to cause it to end.</p>
  
  <p>My understanding of sockets is that they represent endpoints in a stream between the client and the server. When that stream is no longer necessary (i.e, when the client does what it needs to do), I want that stream to be completely removed. I would think that calling end() on both the client and server endpoints of that single stream would achieve this, like sending two FIN messages, but as explained it does not. The reasons I want to do this are so: (a) the client file will actually finish execution and (b) the server will no longer have its socket endpoint of that stream in its system/waste resources listening to it. </p>
  
  <p>Any insight into the source of my problem would be appreciated. </p>