title: How exit socket properly with Nodejs net
body: <p>I have a problem in attempting to kill all processes inside a socket function in nodejs net: loops keep running after the socket disconnects. Is there a way to kill all processes called by the socket?</p>
  <pre><code>net.createServer(function (socket) {
  
    socket.on('end', function () {
      //How kill all processes of the current socket ???
    });
    socket.on('error', function () {
      //How kill all processes of the current socket ???
    });
  
    i=0;
    setInterval(function () {//Waiting other peer for 5sec
      i++;
      console.log(&quot;loop&quot;);
      if(i&gt;=10){
        clearInterval(this);
      }
    }, 1000);
  
  }).listen(1000);
  </code></pre>