title: How to forward websocket connection (Node.js + ws) from port A to port B?
body: <p>Let's say if I have a server running at 8000 port like this:</p>
  
  <pre><code>  var s = http.createServer();
    s.on('request', function(request, response) {
      response.writeHeader(200);
      response.end();
    });
    s.listen(8000);
    var w = new WebSocketServer({
      server: s
    });
  </code></pre>
  
  <p>And then I wish to forward the message received on port 8000 to port 9000:</p>
  
  <pre><code>w.on('connection', function(ws) {
    var remote = null;
  
    ws.on('message', function(data) {
      remote = new WebSocket('ws://127.0.0.1:9000');
      remote.on('open', function() {
        remote.send(data);
      });
    });
    ws.on('close', function() {
      if (remote) {
        return remote.destroy();
      }
    });
    return ws.on('error', function() {
      if (remote) {
        return remote.destroy();
      }
    });
  });
  </code></pre>
  
  <p>Sadly this implementation doesn't seem to work. What would be the right way to do this?</p>