var Connection = require('./connection')

exports.listens = {
  on: {
    data: {
      websocket () {
        this.parent.startWebsocketServer(this.val)
      }
    }
  }
}

exports.properties = {
  websocketServer: true
}

var runnin = false
exports.define = {
  startWebsocketServer (port) {
    if (runnin) return
    runnin = true
    // this is dirty stuff! temp will have to change!
    // now its impossible to change the protocl/server youre using to listen
    if (!this._parent.clients) {
      this._parent.set({clients: {}})
    }
    var adapter = this
    if (adapter.websocketServer) {
      adapter.emit('error', '"websocket server is allready started -- not implemented yet ' + port + '"')
      return
    }
    var connection = new Connection()
    connection.listen(port, 'upstream')
    connection.on('listens', () => {
      adapter.emit('listens', 'websocket')
    })
    connection.on('data', (payload) => {
      adapter.parse(JSON.parse(payload.data), payload.connection)
    })
    connection.on('close', function (data) {
      if (data && data.client) {
        adapter.parent.clients[data.client].remove()
      }
    })
  }
}

@@ -218,16 +218,19 @@
 true%0A%7D%0A%0A
+// 
 var runn
@@ -290,24 +290,27 @@
 (port) %7B%0A   
+ //
  if (runnin)
@@ -310,26 +310,83 @@
 runnin) 
-return%0A   
+%7B%0A    //   throw new Error('xxxxx')%0A    //   return%0A    // %7D%0A    //
  runnin 

remove runnin fix