title: How can I open a TCP socket in node and also use http.request?
body: <p>Using node.js I'm trying to read from a series of HTTP requests, parse the data and then write to a TCP socket for ingestion into a data service.</p>
  
  <p>It seems that when there is a socket open, <code>http.request</code> gets errors.</p>
  
  <p>I have stripped this code down to just the problem itself:</p>
  
  <pre><code>var net = require('net');
  var http = require('http');
  
  var socket = net.createConnection(9997);
  
  var req = http.request("http://www.google.com", function(response) {
      var body = '';
  
      response.on('data', function(c) {
          body += c;
      });
  
      response.on('end', function() {
          console.log(body);
      });
  });
  req.end();
  </code></pre>
  
  <p>This will fail with:</p>
  
  <pre><code>events.js:85 
        throw er; // Unhandled 'error' event
              ^
  Error: connect ECONNREFUSED
      at exports._errnoException (util.js:746:11)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
  </code></pre>
  
  <p>However, if you comment out the line beginning with <code>var socket</code> the http.request call runs just fine.</p>
  
  <p>Can anyone shed any light on why I can't use http.request once a socket is open on an unrelated port?</p>
  
  <p>Thanks!</p>