title: NodeJs: Never emits "end" when reading a TCP Socket
body: <p>I am pretty new to Node.Js and I'm using tcp sockets to communicate with a client. Since the received data is fragmented I noticed that it prints "ondata" to the console more than once. I need to be able to read all the data and concatenate it in order to implement the other functions.  I read the following <a href="http://blog.nodejs.org/2012/12/20/streams2/" rel="nofollow">http://blog.nodejs.org/2012/12/20/streams2/</a> and thought I can use <code>socket.on('end',...)</code> for this purpose. But it never prints "end" to the console.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Client.prototype.send = function send(req, cb) {
      var self = this;
  
      var buffer = protocol.encodeRequest(req);
      var header = new Buffer(16);
      var packet = Buffer.concat([ header, buffer ], 16 + buffer.length);
  
      function cleanup() {
         self.socket.removeListener('data', ondata);
         self.socket.removeListener('error', onerror);
      }
  
      var body = '';
  
      function ondata() {
      var chunk = this.read() || '';
         body += chunk;
         console.log('ondata');
      }
  
      self.socket.on('readable', ondata);
  
      self.socket.on('end', function() {
          console.log('end');
      });
  
     function onerror(err) {
        cleanup();
        cb(err);
     }
  
     self.socket.on('error', onerror);
     self.socket.write(packet);
  };
  </code></pre>