var net = require('net');
var fs = require('fs');
var path = require('path');

var EventTransmitter = require('event-transmitter');


// Main Constructor

function startServer(main, channel) {
  var socketPath = path.join(__dirname, '/' + channel + '.sock');
  var server = net.createServer();

  server.on('error', function(e) {
    if (e.code === 'EADDRINUSE') {
      var clientSocket = new net.Socket({
        readableObjectMode: true
      });

      clientSocket.on('error', function(e) {
        if (e.code === 'ECONNREFUSED') {
          fs.existsSync(socketPath) && fs.unlinkSync(socketPath);
          server.listen(socketPath, function() {
            console.log('server recovered');
          });
        }
      });

      clientSocket.connect({
        path: socketPath
      });
    }
  });

  server.on('connection', function(socket) {
    var stream;
    var eventListener = new EventTransmitter.Listener();

    socket.pipe(eventListener);

    eventListener.on('header', function(header) {
      stream = main.stream(header.id);
      eventListener.pipe(stream);
    });

    eventListener.on('footer', function(footer) {
      if (footer.exitCode !== undefined) {
        stream.exit(footer.exitCode);
      }
    });
  });

  server.listen(socketPath);

  process.on('exit', function(code) {
    console.log('closing server at:', server.address());
    try {
      server.close();
    } catch (e) {
      console.log('cannot close server, may be already closed');
    }
  });
}


module.exports = startServer;

@@ -540,16 +540,20 @@
         
+if (
 fs.exist
@@ -573,11 +573,23 @@
 ath)
- &&
+) %7B%0A           
  fs.
@@ -612,16 +612,29 @@
 tPath);%0A
+          %7D%0A%0A
         

Clean up code (#20)