         @log 'No host defined'
         callback new Error 'No host defined'
              
-Mailbox::loadInbox = (server, callback) ->
-    server.openBox 'INBOX', false, (err, box) =>
-        @log "INBOX opened successfully"
-        callback err, server
- 
+
+Mailbox::openInbox = (callback) ->
+    @connectImapServer (err, server) =>
+        if err
+            server.emit "error", err if server?
+            callback err
+        else
+            server.openBox 'INBOX', false, (err, box) =>
+                @log "INBOX opened successfully"
+                callback err, server
+
+                
+Mailbox::closeBox = (server, callback) ->
+   server.closeBox (err) =>
+        if err
+            @log "cant close box"
+            callback err
+        else
+            server.logout =>
+                @log "logged out from IMAP server"
+                callback()
+
+
 Mailbox::fetchMessage = (server, mailToBe, callback) ->
     
     if typeof mailToBe is "string"

            messageFlags = message.flags
            do parser.end
     

Mailbox::getNewMail = (job, callback, limit=250) ->
    

    # global vars
    mailbox = @
 
    @openInbox (err, server) ->
        return emitOnErr server, err if err
        loadInboxMails server
              
    emitOnErr = (server, err) ->
        if err
