         version: "1" # <--- this should always be a string, dont ask me why, its not an integer
         hostname: "aybarss-MacBook-Air.local"
         publicIP:""
-        port:"62428"
+        port: @options.port.toString() or null # <-- port should be string.
       payload.withArgs = args
       payload.links = []
 


      @ws.on 'message', (data, flags)->
        # now we descrub message and call our callback function
        scrubbed = JSON.parse data
        scrubber = new Scrubber @localStore
        unscrubbed = scrubber.unscrub scrubbed
        callback = self.localStore.get(scrubbed.method)
        if callback
            callback.apply this, unscrubbed

    callRemote: (method, args, authentication, callback=noop)=>
      scrubber = new Scrubber @localStore
      # adding required stuff
      payload = {}
      payload.kite =
        name: "application"
        username: "devrim"
        id: "a8877ba5-9b18-420b-792a-e561316b3257"
        environment: 'development'

        (err, kites)->
            callback err, kites

class KiteServer extends EventEmitter
  constructor: ()->
    @_name = @constructor._name
    throw "every worker needs a name" if not @_name? and not @_name
    @localStore = new Store
    @remoteStore = new Store

  callRemote: (client, method, args)=>
      scrubber = new Scrubber @localStore
      # adding required stuff
      payload = args
      scrubber.scrub payload, =>
        console.log "!!!! 1"
        scrubbed = scrubber.toDnodeProtocol()
        scrubbed.method = method
        console.log "2"
        message = JSON.stringify scrubbed
        console.log "3", message
        client.send message

  runServer: (@host="localhost", @port=9999)->
    self = this
    @wss = new WebSocketServer host:@host, port: @port
    @kontrolClient = new KiteClient name:@_name, port: @port
    @kontrolClient.on 'connected', ()=>
      console.log "connecting to kontrol"
      @kontrolClient.callRemote "register", null, null, (err, data)->
        console.log "received data", data
    @kontrolClient.connect()

    @wss.on 'connection', (client)->
      client.on 'message', (data)->
        console.log('received: %s', data)
        #client.send('something')
        data = data.replace(/withArgs/ig, 'withArgs')
        scrubbed = JSON.parse data
        scrubber = new Scrubber self.localStore
        unscrubbed = scrubber.unscrub scrubbed, (callbackId)->
          unless self.remoteStore.has(callbackId)
            self.remoteStore.add callbackId, (args...)->
              console.log ".....................", callbackId, args
              self.callRemote client, callbackId, args
          self.remoteStore.get callbackId

        if self.constructor.prototype.propertyIsEnumerable(scrubbed.method) and 'function' is typeof self[scrubbed.method]
          console.log ">>>", "here is your callback"
          callback = self[scrubbed.method]
        else
          callback = self.localStore.get(scrubbed.method)

        if callback
            callback.apply self, unscrubbed


# server example
class MathWorker extends KiteServer
  @_name = 'mathworker'
  square: (withArgs, callback)->
    console.log "square called !", arguments
    callback null, 1

mathworker = new MathWorker
mathworker.runServer()

# # this is just an example for a client
# k = new KiteClient
# k.on 'connected', ()->
#     console.log "connected...."
#     k.getKites (err, kites)->
#       if err or not Array.isArray(kites) or kites.length == 0
#         throw "no available kite worker"
#       # connecting to first avail kite
#       kite = kites[0]
#       console.log ">>>>", kite.kite.publicIP
#       url = "ws://#{kite.kite.publicIP}:#{kite.kite.port}/dnode"
#       console.log url
#       authentication = 
#         type: 'token'
#         key: kite.token
#       mathworker = new KiteClient url
#       mathworker.on 'connected', ()->
#         setInterval ()->
#           value = Math.random() * 100
#           console.log "sending #{value} to mathworker"
#           mathworker.callRemote "square", value, authentication, (err, data)->
#             console.log "response from mathworker", err, data
#         , 5000
#       mathworker.connect()
# k.connect()