 
     save: ->
         if not @session.alive
-            console.log "[ratom] Error saving #{path.basename @tempfile} to #{@remoteAddress}"
-            status-message.display "Error saving #{path.basename @tempfile} to #{@remoteAddress}", 2000
+            console.log "[ratom] Error saving #{path.basename @tempfile} to #{@remote_address}"
+            status-message.display "Error saving #{path.basename @tempfile} to #{@remote_address}", 2000
             return
-        console.log "[ratom] saving #{path.basename @tempfile} to #{@remoteAddress}"
-        status-message.display "Saving #{path.basename @tempfile} to #{@remoteAddress}", 2000
+        console.log "[ratom] saving #{path.basename @tempfile} to #{@remote_address}"
+        status-message.display "Saving #{path.basename @tempfile} to #{@remote_address}", 2000
         @session.send "save"
-        @session.send "token: #{@token}"
+        @session.send "token: #{@settings['token']}"
         data = fs.readFileSync(@tempfile)
         @session.send "data: " + Buffer.byteLength(data)
         @session.send data
 
     close: ->
         console.log "[ratom] closing #{path.basename @tempfile}"
-        # try to close session
-        @session.close()
         @subscriptions.dispose()
+        @session.try_close()
 
 class Session
-    should_parse_data: false
-    readbytes: 0
-    nconn: 0
-
     constructor: (socket) ->
+        @should_parse_data = false
+        @nconn = 0
         @socket = socket
         @send "Atom "+ atom.getVersion()
         @alive = true

        mkdirp.sync(dirname)
        @fd = fs.openSync(@tempfile, 'w')

    write: (str) ->
        fs.writeSync(@fd, str)

    open: ->
        fs.closeSync @fd
        console.log "[ratom] opening #{@tempfile}"
        # register events
        atom.workspace.open(@tempfile, activatePane:true).then (editor) =>


    parse_line: (line) ->
        if @should_parse_data
            @file.append(line)
            if @file.closed
                @should_parse_data = false
                @file.open()

        else if line.match /open\n/
            @file = new FileHandler(@)
            @nconn += 1
        else
            @parse_setting(line)

    parse_setting: (line) ->
        m = line.match /([a-z\-]+?)\s*:\s*(.*?)\s*$/
        if m and m[2]?
            @file.set(m[1], m[2])
            if m[1] == "data"
                @file.create()
                @should_parse_data = true

    send: (cmd) ->
        @socket.write cmd+"\n"

    try_close: ->
        @nconn -= 1
        if @alive and @nconn == 0
            @alive = false
