title: Node js net tcp client
body: <p>Following is a net tcp client code</p>
  
  <pre><code>var net = require('net');
  var client = new net.Socket();
  client.connect(1337, '127.0.0.1', function() {
     console.log('Connected');
     client.write('Hello server');
  });
  
  client.on('data', function(data) {
     console.log('Received: ' + data);
     client.destroy(); // kill client after server's response
  });
  
  client.on('close', function() {
     console.log('Connection closed');
  });
  </code></pre>
  
  <p>client.write('Hello server') is written inside the client.connect scope.
  Is there a way to use it outside the client.connect scope.</p>
  
  <p>I tried the following code but failed to send anything to server. However the return value of client.write returned true.</p>
  
  <pre><code>var net = require('net');
  var client = new net.Socket();
  client.connect(1337, '127.0.0.1', function() {
     console.log('Connected');
  });
  
  client.write('Hello server');  //Did not work
  </code></pre>