title: Receive TCP Messages without closing the connection
body: <p>i need some help for my project. I need to receive TCP Messages in nodejs and send them to a web interface via Jquery Ajax. </p>
  
  <p>The Requests are triggerd in the webinterface and send via ajax to the node.js server. This one send a request to tcp server (c++) and pass the answer to the webui.</p>
  
  <p>I made a function for test purporses which includes all necessary tasks.
  function tcp_allinone(req,res){</p>
  
  <pre><code>    var vRequest = req.body.jsonmsg;
      console.log("&lt; Info &gt; tcp_allinone " + vRequest);
  
      if (tcpcl == undefined) {
          tcpcl = new net.Socket();
      } 
  
      tcpcl.connect(objTCPSocket.Port, objTCPSocket.Host, function () {
          tcpcl.write(vRequest);
  
      });
  
      tcpcl.on('data', function (data) {
          res.contentType('json');
          res.send({ data: data.toString() });
          tcpcl.end();
      });
  
      tcpcl.on('error', function (error) {
          res.contentType('json');
          res.send({ data: error.toString() });
          tcpcl.destroy();
      });
  
  
      tcpcl.on('close', function () {
          tcpcl.destroy();
          tcpcl = undefined;
      });
  </code></pre>
  
  <p>}</p>
  
  <p>For this project the connection must be hold open and it is not wanted to reopen the connection for each request. In consequence i write to functions, one to start the socket and open the connection, the second to write and receiving messages:</p>
  
  <p>function tcp_starter(req,res){</p>
  
  <pre><code>    if (tcpcl == undefined) {
          tcpcl = new net.Socket();
      } 
  
      tcpcl.connect(objTCPSocket.Port, objTCPSocket.Host, function () {
          console.log("&lt; Info &gt; client_connect ");
          res.contentType('json');
          res.send({ data: "&lt; Info &gt; client_connect" });
      });
  
      tcpcl.on('error', function (error) {
          res.contentType('json');
          res.send({ data: error.toString() });           
          tcpcl.destroy();
      });
  
      tcpcl.on('close', function () {
          tcpcl.destroy();
          tcpcl = undefined;
      });
  
  }
  </code></pre>
  
  <p>function tcp_writelisten(req,res){</p>
  
  <pre><code>        var vRequest = req.body.jsonmsg;
          console.log("&lt; Info &gt; tcp_writelisten " + vRequest);
  
          if (tcpcl !== undefined) {
              tcpcl.write(vRequest, function () {
                  console.log("&lt; Info &gt; tcp_write: " + vRequest);
              });
          }
  
          tcpcl.on('data', function (data) {
              res.contentType('json');
              res.send({ data: data.toString() });
              console.log("&lt; Info &gt; tcp_listen: " + data.toString());
          });
  </code></pre>
  
  <p>}</p>
  
  <p>tcp_starter is called at the beginning, tcp_writerlisten each two seconds to get new strings from the server. But with this construct i get the following error message:
  ttp.js:707
      throw new Error('Can\'t set headers after they are sent.');
            ^
  Error: Can't set headers after they are sent.
      at ServerResponse.OutgoingMessage.setHeader (http.js:707:11)</p>
  
  <p>I guess the problem is, that with every function call tcp_writelisten i establish a new Eventlistener and so on i get i++ Receiving messages. </p>
  
  <p>How can i destroy the eventlistener on('data' after receiving without reconnecting? How can i improve this structure?</p>
  
  <p>Big thanks for your help</p>