'use strict';
try {
  var memwatch = require('memwatch');
  memwatch.on('leak', function(info) {
    console.log("leak", info);
  });
  console.log("Watching for memory leaks");
} catch (e) {
  console.log("Not watching for memory leaks -- npm install memwatch to do so");
}

var config = {
  port: 1337
};

var connectedDevices = {};

var bindSparkEvents = function(spark) {
  spark.on('my identity', function(data) {
    connectedDevices[data.macAddress] = spark;
    console.log(Object.keys(connectedDevices));
  });
};

var Primus = require('primus')
  , http = require('http')
  , server = http.createServer()
  , primusSpec = { transformer: 'sockjs', parser: 'json' }
  , primus = new Primus(server, primusSpec);

primus.on('connection', function connection(spark) {
  spark.on('data', function (data) {
    if (spark.reserved(data.args[0])) return;
    spark.emit.apply(spark, data.args);
  });
  console.log("New device connected");
  bindSparkEvents(spark);
  spark.write({
    args: ["please identify"]
  });
});

server.listen(config.port);
console.log("Listening on "+config.port);

@@ -297,24 +297,46 @@
 t: 1337%0A%7D;%0A%0A
+// Move this to Redis%0A
 var connecte
@@ -352,199 +352,308 @@
 = %7B%7D
-;%0A%0Avar bindSparkEvents = function(spark
+%0A  , connectedBrowsers = %7B%7D;%0A%0Avar cluster = require('cluster');%0Aif (cluster.isMaster
 ) %7B%0A
+%0A
   
-spark.on('my identity', function(data) %7B%0A    connectedDevices%5Bdata.macAddress%5D = spark;%0A    console.log(Object.keys(connectedDevices));%0A  %7D);%0A%7D;%0A%0A
+// One worker for now -- but later can check # of cpus%0A  cluster.fork();%0A%0A  cluster.on('disconnect', function(worker) %7B%0A    console.error(%22disconnect!%22);%0A    cluster.fork();%0A  %7D);%0A%0A%7D else %7B%0A  // the worker%0A%0A  
 var 
@@ -679,16 +679,18 @@
 rimus')%0A
+  
   , http
@@ -708,16 +708,51 @@
 'http')%0A
+    , domain = require('domain')%0A  
   , serv
@@ -776,16 +776,18 @@
 erver()%0A
+  
   , prim
@@ -837,16 +837,18 @@
 json' %7D%0A
+  
   , prim
@@ -885,16 +885,18 @@
 Spec);%0A%0A
+  
 primus.o
@@ -940,16 +940,986 @@
 park) %7B%0A
+%0A    var d = domain.create();%0A%0A    d.on('error', function(err) %7B%0A      console.log(%22error%22, err.stack);%0A      try %7B%0A        // make sure we close down within 30 seconds%0A        var killtimer = setTimeout(function() %7B%0A          process.exit(1);%0A        %7D, 5000);%0A        // But don't keep the process open just for that!%0A        killtimer.unref();%0A%0A        // stop taking new requests.%0A        server.close();%0A%0A        // Let the master know we're dead.  This will trigger a%0A        // 'disconnect' in the cluster master, and then it will fork%0A        // a new worker.%0A        cluster.worker.disconnect();%0A%0A        // try to send an error to the request that triggered the problem%0A        spark.write(%7B%0A          args: %5B%22relay error%22, err.stack%5D%0A        %7D);%0A      %7D catch (er2) %7B%0A        // oh well, not much we can do at this point.%0A        console.error('Error sending error report to client', er2.stack);%0A      %7D%0A    %7D);%0A%0A    d.add(spark);%0A%0A    d.run(function() %7B%0A    
   spark.
@@ -1951,16 +1951,20 @@
 ) %7B%0A    
+ 
+   
 if (spar
@@ -1997,16 +1997,20 @@
 return;%0A
+    
     spar
@@ -2047,77 +2047,244 @@
 ;%0A  
-%7D);%0A  console.log(%22New device connected%22);%0A  bindSparkEvents(spark);%0A
+    %7D);%0A%0A      spark.on('i am a netpocketos device', function(token, info) %7B%0A        connectedDevices%5Btoken%5D = %7B%0A          info: info,%0A          spark: spark%0A        %7D;%0A        console.log(Object.keys(connectedDevices));%0A      %7D);%0A%0A    
   sp
@@ -2295,24 +2295,28 @@
 write(%7B%0A    
+ 
+   
 args: %5B%22plea
@@ -2335,17 +2335,33 @@
 %5D%0A  
-%7D);%0A%7D);%0A%0A
+    %7D);%0A    %7D);%0A  %7D);%0A%0A  
 serv
@@ -2384,16 +2384,18 @@
 .port);%0A
+  
 console.
@@ -2416,20 +2416,23 @@
  on %22+config.port);%0A
+%7D%0A%0A

Add resilience using cluster and domain