title: Send commands over socket, but wait every time for response (Node.js)
body: <p>I need to send several commands over telnet to a server. If I try to send them without a time delay between every command, the server freaks out:</p>
  
  <pre><code>var net = require('net');
  var conn = net.createConnection(8888, 'localhost');
  conn.on('connect', function() {
      conn.write(command_1);
      conn.write(command_2);  
      conn.write(command_3);
      //...
      conn.write(command_n);
  })
  </code></pre>
  
  <p>I guess the server needs some time to respond to command <em>n</em> before I send it command <em>n+1</em>. One way is to write something to the log and fake a "wait":</p>
  
  <pre><code>var net = require('net');
  var conn = net.createConnection(8888, 'localhost');
  conn.on('connect', function() {
      console.log('connected to server');
      console.log('I'm about to send command #1');
      conn.write(command_1);
      console.log('I'm about to send command #2');
      conn.write(command_2);  
      console.log('I'm about to send command #3');
      conn.write(command_3);
      //...
      console.log('I'm about to send command #n');
      conn.write(command_n);
  })
  </code></pre>
  
  <p>It might also be the fact that <strong><code>conn.write()</code> is asynchronous, and putting one command after another doesn't guranty the correct order??</strong></p>
  
  <p>Anyway, what is the correct pattern to assure correct order and enough time between two consecutive commands, for the server to respond?</p>