title: Why Node js tcp server not detecting disconnected client
body: <p>I am trying to communicate server-client communication using TCP-IP in node js</p>
  
  <p>Below is my server side code and I have a GSM device which acts as a client.When GSM device connects to the server I get the message that device is connected! but when I cut off the power supply of GSM device then the server should recognize that device is disconnected but no any message displays on screen even if I have code for the disconnect event.</p>
  
  <p>server code</p>
  
  <pre><code>// Load the TCP Library
  net = require('net');
  
  // Keep track of the chat clients
  var clients = [];
  
  // Start a TCP Server
  net.createServer(function (socket) {
  
  // Identify this client
  socket.name = socket.remoteAddress + ":" + socket.remotePort 
  
   // Put this new client in the list
   clients.push(socket);
  
   // Send a nice welcome message and announce
   socket.write("Welcome " + socket.name + "\n");
   broadcast(socket.name + " Device is connected!\n", socket);
  
   // Handle incoming messages from clients.
   socket.on('data', function (data) {
   broadcast(socket.name + "&gt; " + data, socket);
   });
  
   // Remove the client from the list when it leaves
   socket.on('end', function () {
   clients.splice(clients.indexOf(socket), 1);
   broadcast(socket.name + " Device left.\n");
   });
  
   // Send a message to all clients
   function broadcast(message, sender) {
   clients.forEach(function (client) {
    // Don't want to send it to sender
    if (client === sender) return;
    client.write(message);
    });
    // Log it to the server output too
    process.stdout.write(message)
    }
  
    }).listen(5000);
  
    // Put a friendly message on the terminal of the server.
    console.log("Chat server running at port 5000\n");
  </code></pre>