var WebSocket = require('ws');
var reHttpSignalhost = /^http(.*)$/;
var reTrailingSlash = /\/$/;

function connect(signalhost) {
  // if we have a http/https signalhost then do some replacement magic to push across
  // to ws implementation (also add the /primus endpoint)
  if (reHttpSignalhost.test(signalhost)) {
    signalhost = signalhost
      .replace(reHttpSignalhost, 'ws$1')
      .replace(reTrailingSlash, '') + '/primus';
  }

  return new WebSocket(signalhost);
}

module.exports = function(signalhost, opts, callback) {
  var ws = connect(signalhost);

  ws.once('open', function() {
    // close the test socket
    ws.close();

    callback(null, {
      connect: connect
    });
  });
};

@@ -89,16 +89,104 @@
 = /%5C/$/;
+%0Avar pingers = %5B%5D;%0Avar PINGHEADER = 'primus::ping::';%0Avar PONGHEADER = 'primus::pong::';
 %0A%0Afuncti
@@ -210,16 +210,31 @@
 host) %7B%0A
+  var socket;%0A%0A
   // if 
@@ -541,14 +541,16 @@
 %0A%0A  
-return
+socket =
  new
@@ -573,17 +573,530 @@
 lhost);%0A
-%7D
+%0A  socket.on('message', function(data) %7B%0A    if (data.slice(0, PONGHEADER.length) === PONGHEADER) %7B%0A      queuePing(socket);%0A    %7D%0A  %7D);%0A%0A  queuePing(socket);%0A  return socket;%0A%7D%0A%0Afunction queuePing(socket) %7B%0A  pingers.push(socket);%0A%7D%0A%0AsetInterval(function() %7B%0A  pingers.splice(0).forEach(function(socket) %7B%0A    if (socket.readyState === 1) %7B%0A      socket.send(PINGHEADER + Date.now(), function(err) %7B%0A        if (err) %7B%0A          console.log('could not send ping: ', err);%0A        %7D%0A      %7D);%0A    %7D%0A  %7D);%0A%7D, 10e3);
 %0A%0Amodule

Improve node socket connection to ping as per what a primus server expects