     @watchRef       = @workspaceRef.child "watch"
     @usersRef       = @workspaceRef.child "users"
     @userRef        = @usersRef.child KD.nick()
+    @requestPingRef = @workspaceRef.child "requestPing"
     @sessionKeysRef = @firebaseRef.child  "session_keys"
 
-  bindRemoteEvents: ->
+  syncWorkspace: ->
     @workspaceRef.once "value", (snapshot) =>
       if @getOptions().sessionKey
         unless snapshot.val()?.keys
           @showNotActiveView()
           return false
 
-      cb = =>
-        isOldSession = keys = snapshot.val()?.keys
-        if isOldSession
-          @isOldSession = yes
-          @sessionData  = keys
-          @createPanel()
-        else
-          @createPanel()
-          @workspaceRef.set "keys": @sessionData
-
-        if not isOldSession
-          @addToHistory { message: "$0 started the session", by: KD.nick() }
-
-        @setPresenceHandlers()
-        @watchRef.child(@nickname).set "everybody"
-        @sessionKeysRef.child(@nickname).set @sessionKey
-        @userRef.child("status").set "online"
-
-        @loader.destroy()
-        @chatView?.show()
-
-        @emit "WorkspaceSyncedWithRemote"
-
-      if @amIHost() then cb()
+      isOldSession = keys = snapshot.val()?.keys
+      if isOldSession
+        @isOldSession = yes
+        @sessionData  = keys
+        @createPanel()
       else
-        @pingHost (status) =>
-          cb()  if status is "online"
+        @createPanel()
+        @workspaceRef.set "keys": @sessionData
+
+      if not isOldSession
+        @addToHistory { message: "$0 started the session", by: KD.nick() }
+
+      @watchRef.child(@nickname).set "everybody"
+      @sessionKeysRef.child(@nickname).set @sessionKey
+      @userRef.child("status").set "online"
+
+      @loader.destroy()
+      @chatView?.show()
+
+      @emit "WorkspaceSyncedWithRemote"
+
+  bindRemoteEvents: ->
+    if @amIHost() then @syncWorkspace()
+    else
+      @requestPingFromHost()
 
     @usersRef.on "child_added", (snapshot) =>
       @fetchUsers()

      @usersRef.child(username).child("status").on "value", (snapshot) =>
        status = snapshot.val()
        if status is "online"
          @emit "SomeoneJoinedToTheSession", username
          if @amIHost()
            message = "#{username} joined the session"
            @addToHistory { message, by: KD.nick() }
        else if status is "offline"
          @emit "SomeoneHasLeftTheSession", username
          if @amIHost()
            message = "#{username} has left the session"


    new KDNotificationView options

  setPresenceHandler: ->
    if @amIHost()
      @presenceInterval = @utils.repeat 10000, =>
        @userRef.child("timestamp").set Date.now(), (err) =>
          return  unless err
          @utils.killRepeat @presenceInterval
    else
      @presenceInterval = @utils.repeat 10000, @bound "pingHost"

    @once "KDObjectWillBeDestroyed", =>
      @killPresenceTimer()

  checkHost: (snapshot, callback) ->
    host = snapshot.val()
    return @showNotActiveView()  unless host

    diff = Date.now() - host.timestamp

    if diff <= 15000
      @hideNotification()
      @hostStatus = "online"
    else if diff >= 30000
      @hideNotification()
      @showNotActiveView()
      @hostStatus = "offline"
    else if diff >= 15000
      @showNotification "Host is experiencing connectivity issues"
      @hostStatus = "unknown"

