var net = require('net'),
    path = require('path'),
    assert = require('assert'),
    spawn = require('child_process').spawn,
    async = require('async'),
    jsonStream = require('json-stream'),
    cb = require('assert-called');

var PORT0 = 5434,
    PORT1 = 5435,
    gotFirstConnection = false,
    firstClosed = false,
    child;

var server0 = net.createServer(cb(function (socket) {
  var stream = socket.pipe(jsonStream());

  console.log('got first connection');

  gotFirstConnection = true;

  stream.on('readable', cb(function () {
    var chunk = stream.read();
    if (chunk && !firstClosed) {
      console.log('closing first server');
      server0.close();
      socket.destroy();
      firstClosed = true;
    }
  }));
}));

var server1 = net.createServer(cb(function (socket) {
  var stream = socket.pipe(jsonStream());

  console.log('got second connection');
  assert(gotFirstConnection);

  stream.on('readable', cb(function () {
    var chunk = stream.read();
    if (chunk) {
      child.kill();
      process.exit();
    }
  }));
}));

server1.listen(PORT1, cb(function () {
  server0.listen(PORT0, cb(function () {
    child = spawn(
      path.join(__dirname, '..', 'forza'),
      [
        '-h', '127.0.0.1:' + PORT0.toString(),
        '-h', '127.0.0.1:' + PORT1.toString(),
        '--', 'node', path.join(__dirname, 'fixtures', 'listen.js')
      ]
    );

    setTimeout(function () {
      console.log('timeout');
      child.kill();
      process.exit(1);
    }, 20000);
  }));
}));

@@ -270,25 +270,17 @@
 35,%0A    
-gotFirstC
+c
 onnectio
@@ -280,24 +280,21 @@
 nnection
+s
  = 
-false
+0
 ,%0A    fi
@@ -328,51 +328,29 @@
 d;%0A%0A
-var server0 = net.createServer(cb(function 
+function onConnection
 (soc
@@ -388,32 +388,53 @@
 pe(jsonStream())
+,%0A      server = this
 ;%0A%0A  console.log
@@ -439,22 +439,16 @@
 og('got 
-first 
 connecti
@@ -454,23 +454,55 @@
 ion'
+, this._name
 );%0A
-%0A
   
-gotFirstC
+++connections;%0A  console.log(c
 onne
@@ -506,23 +506,18 @@
 nnection
- = true
+s)
 ;%0A%0A  str
@@ -591,21 +591,96 @@
     if (
+!
 chunk
+) %7B%0A      return;%0A    %7D%0A    console.dir(chunk);%0A%0A    if (connections === 1
  && !fir
@@ -714,28 +714,20 @@
 og('
-closing first server
+first closed
 ');%0A
@@ -738,17 +738,16 @@
   server
-0
 .close()
@@ -810,118 +810,99 @@
 %7D%0A  
-%7D));%0A%7D));%0A%0Avar server1 = net.createServer(cb(function (socket) %7B%0A  var stream = socket.pipe(jsonStream());%0A%0A
+  else if (connections %3E= 2) %7B // For some reason, %60connections === 2%60 wouldn't work%0A   
   
+ 
 cons
@@ -914,209 +914,220 @@
 og('
-got second connection');%0A  assert(gotFirstConnection);%0A%0A  stream.on('readable', cb(function () %7B%0A    var chunk = stream.read();%0A    if (chunk) %7B%0A      child.kill();%0A      process.exit();%0A    %7D%0A  %7D));%0A%7D
+complete');%0A      server.close();%0A      socket.destroy();%0A      child.kill();%0A      process.exit();%0A    %7D%0A  %7D));%0A%7D%0A%0Avar server0 = net.createServer(cb(onConnection)),%0A    server1 = net.createServer(cb(onConnection
 ));%0A

Fix `failover-on-write` test