 # Loads the connection data from local storage.
 restoreData = ->
   if sessionStorage?
-    for k in [ "jid", "sid", "rid", "host", "port", "path" ]
+    for k in [ "jid", "sid", "rid", "host", "port", "path", "protocol" ]
       j = key2cache k
       connection_options[k] = sessionStorage[j]  if sessionStorage[j]
 


    # case 1
    if opt.host and opt.port and opt.path
      return "http://#{ opt.host }:#{ opt.port }/#{ opt.path }"

    # case 2
    if opt.host and opt.port and not opt.path
      return "http://#{ opt.host }:#{ opt.port }/#{ DEFAULT_PATH }"

    # case 3
    if opt.host and not opt.port and opt.path
      return "http://#{ opt.host }/#{ opt.path }"

    # case 4
    if opt.host and not opt.port and not opt.path
      return "http://#{ opt.host }/#{ DEFAULT_PATH }"

    # case 5
    if not opt.host and opt.port and opt.path
      return "http://#{ domain }:#{ opt.port }/#{ opt.path }"

    # case 6
    if not opt.host and opt.port and not opt.path
      return "http://#{ domain }:#{ opt.port }/#{ DEFAULT_PATH }"

    # case 7
    if not opt.host and not opt.port and opt.path
      return "http://#{ domain }/#{ opt.path }"

    # case 8
    if not opt.host and not opt.port and not opt.path
      return "http://#{ domain }/#{ DEFAULT_PATH }"

  # fallback
  "http://#{ domain }/#{ DEFAULT_PATH }"

login = (jid, pw, opt) ->

  if opt?
    connection_options.path     = opt.path     if opt.path
    connection_options.port     = opt.port     if opt.port
    connection_options.host     = opt.host     if opt.host
    connection_options.protocol = opt.protocol if opt.protocol

  connection = create_connection_obj()
  connection.connect jid, pw, on_connection_change
