     handleEnd: =>
       @emit 'end'
 
-    handleServerDown: ->
+    handleServerDown: =>
+      @emit 'error', 'Server down'
 
+    # The server has send information for initialization.
+    # The state and the client id have been send and will be set.
+    # For each client already sharing a new cursor is created.
+    # Emit an event for signaling the connection is established.
     initialize: (transform) =>
       @emit types.clear
       @id       = transform.clientId

    selectionChanged: (transform) ->
      @sendToServer transform

    changeText: (transform) ->
      @state.set transform.id, transform.state.get transform.id
      @emit types.textChange, transform

    updateCursor: (transform) ->
      @emit types.updateCursor, transform

    changeSelection: (transform) ->
      @emit types.selectionChanged, transform

    newClient: (transform) ->
      @clients.push transform.client
      @state.add transform.client.id
      @emit types.newUser,
        clientId:   transform.client.id
        username:   transform.username
        color:      transform.client.color

    clientLeft: (transform) ->
      @emit types.userLeft,
        clientId: transform.client.id
      initialized = true
      @emit types.initialized

    # Manage acknowledges
    acknowledge: (ack) =>
      for i in [0...@notAcked.length]
        if @notAcked[i].state[@id] is ack[@id]
