var net = require('net')
var fs = require('fs')
var _ = require('icebreaker')
require('icebreaker-peer')

var connection = function(original) {
  original.setKeepAlive(true)
  original.setNoDelay(true)
  this.connection(_.net(original))
}

_.mixin({
  net : function(connection) {
    var net = _.pair(connection)
    if (connection.remoteAddress) net.address = connection.remoteAddress
    if (connection.remotePort) net.port = connection.remotePort
    return net
  }
})

if (!_.peers) _.mixin({ peers : {} })

_.mixin({
  net : _.peer({
    name : 'net',
    auto : true,
    start : function() {
      var server = this.server = net.createServer(connection.bind(this))
      this.server.on('error', function(err) {
        if (typeof this.port === 'string' && err.code === 'EADDRINUSE') {
          var socket = net.Socket()

          socket.on('error', function(err) {
            if (err.code == 'ECONNREFUSED') {
              fs.unlink(this.port, function(err) {
                if (err)
                  _(
                    'cannot remove unix socket ' + this.port,
                    _.log(process.exit.bind(null, 1), 'emerg')
                  )
                listen()
              }.bind(this))
            }
          }.bind(this))

          socket.connect(this.port, function() {
            _(
              'peer ' + this.name + ' port ' + this.port +
              ' is already in use by another process.',
              _.log(process.exit.bind(null, 1), 'emerg')
            )
          }.bind(this)
          )

          return
        }

        _(
          [ 'cannot start peer' + this.name + ' on port ' + this.port, err ],
          _.log(process.exit.bind(null, 1), 'emerg')
        )
      }.bind(this))

      var onListening = function() {
        if (typeof this.port === 'string') fs.chmod(this.port, 0777)
        this.emit('started')
      }.bind(this)

      var listen = function(onListening) {
        this.server.listen(
          this.port, typeof this.port === 'string' ? null :
          this.address, onListening
        )
      }.bind(this)

      listen(onListening)
    },

    connect : function(params) {
      if (!params.address) params.address = this.address
      process.nextTick(function() {
        connection.call(this,
          net.createConnection(typeof params.port === 'string' ? params.port : {
            port : params.port,
            host : params.address
          })
        )
      }.bind(this))
    },

    stop : function() {
      try {
        this.server.close(function() {
          this.emit('stopped')
        }.bind(this))
      }
      catch (e) {
        _([ e ], _.log(function() { this.emit('stopped') }.bind(this)), 'error')
      }
    }
  })
}, _.peers)

@@ -71,16 +71,27 @@
 eaker')%0A
+if(!_.peer)
 require(
@@ -2232,77 +2232,15 @@
     
-process.nextTick(function() %7B%0A        connection.call(this,%0A         
+var c =
  net
@@ -2257,16 +2257,17 @@
 nection(
+ 
 typeof p
@@ -2291,16 +2291,24 @@
 tring' ?
+%0A       
  params.
@@ -2326,18 +2326,16 @@
         
-  
 port : p
@@ -2338,34 +2338,32 @@
  : params.port,%0A
-  
           host :
@@ -2388,30 +2388,54 @@
     
- 
-   %7D)
+%7D,
 %0A      
-  )%0A     
+function() %7B connection.call(this, c)
  %7D.b
@@ -2439,24 +2439,30 @@
 %7D.bind(this)
+%0A     
 )%0A    %7D,%0A%0A  
@@ -2470,32 +2470,37 @@
  stop : function
+ stop
 () %7B%0A      try %7B
@@ -2526,38 +2526,230 @@
 r.close(function
-() %7B%0A 
+ close() %7B%0A         if(Object.keys(this.connections).length%3E0)%7B%0A          process.nextTick(function()%7B%0A            close.call(this)%0A          %7D.bind(this))%0A          return%0A          %7D%0A        else%0A
          this.em

Update for connect and stop