title: Nodejs simple socket connection doesn't work
body: <p>Every 2 Seconds the client requests data <code>app.get("/query_data"..</code>
   I'm connecting to a specific IP Adress + Port.</p>
  
  <p>After connecting, I'm sending a string to this adress to receive data from it. This works well, but only once.</p>
  
  <pre><code>var socket = new net.Socket();
  app.get("/query_data", function (req, res) {
    socket.connect(2711, '192.168.1.173', function (err) {
      if (err) {
        console.log(err);
      }
      else {
        socket.write("d"); // send "d" to trigger socket.on("data")
      }
    });
  
    socket.on('data', function (data) {
      res.send(data);
    });
  });
  </code></pre>
  
  <p>Error message I receive: </p>
  
  <blockquote>
    <p>Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent
    to the client</p>
  </blockquote>
  
  <p>However, when I put the <code>var socket = new net.Socket();</code> inside <code>app.get()</code> it works.</p>
  
  <pre><code>app.get("/query_data", function (req, res) {
    var socket = new net.Socket();
    socket.connect(2711, '192.168.1.173', function (err) {
      if (err) {
        console.log(err);
      }
      else {
        socket.write("d");
      }
    });
  
    socket.on('data', function (data) {
      res.send(data);
    });
  });
  </code></pre>
  
  <p>Why is that? I don't want to create a new socket everytime the client requests data from the server.</p>
  
  <p>Client side, its wrapped in a 2sec <code>setTimeout()</code> Function:</p>
  
  <pre><code>  $.get("/query_data", function (data) {
        data = data.replace(/[\[\]&amp;]+|nan/g, 'null');
        console.log(JSON.parse(data));
        (....)
    });
  </code></pre>