diff --git dxmpp.module dxmpp.module
index bf1bc2f..e79dec4 100644
--- dxmpp.module
+++ dxmpp.module
@@ -180,6 +180,9 @@ function dxmpp_autoload($class_name) {
     case 'dxmppUserDeregister':
       module_load_include('inc', 'dxmpp', 'includes/dxmpp.userDeregisterClass');
       break;
+    case 'dxmppUserBoshPrebind':
+      module_load_include('inc', 'dxmpp', 'includes/dxmpp.userBoshPrebindClass');
+      break;
   }
 }
 
diff --git themes/dxmpp.theme.inc themes/dxmpp.theme.inc
index b1c2f65..1799323 100644
--- themes/dxmpp.theme.inc
+++ themes/dxmpp.theme.inc
@@ -20,28 +20,25 @@ function template_preprocess_dxmpp_chat(&$variables) {
     return '';
   }
 
-/*  if (!isset($_SESSION['dxmpp_session_id'])) {
-    // Set up a sufficiently large RID that will be incremented gradually
-    // across page loads.
-    //$_SESSION['dxmpp_request_id'] = rand(1000, 10000000);
-    
-    	$dxmpp_http_result = dxmpp_post_request('http://admin%40localhost:admin@localhost:5280/warm/', 'jid='.$user->dxmpp->xmpp_username.'@localhost&password='.$user->dxmpp->xmpp_password);
-    	$doc = simplexml_load_string($dxmpp_http_result);
+  global $user;
+  if ($user == 0) {
+    unset($_SESSION['dxmpp_session_id']);
+  }
+  if (!isset($_SESSION['dxmpp_session_id'])) {
    
-    //$conn = new dxmppUserConnect($user->dxmpp->xmpp_username, $user->dxmpp->xmpp_password, $_SESSION['dxmpp_request_id']);
-
-    // @TODO: Grab the proper  SID.
-    	$_SESSION['dxmpp_request_id'] = (int)$doc->body->dl->dd[2][0];
-    	$_SESSION['dxmpp_session_id'] = (string)$doc->body->dl->dd[1][0];
-    	$_SESSION['dxmpp_session_jid'] = (string)$doc->body->dl->dd[0][0];
+    $conn = new dxmppUserBoshPrebind($user->dxmpp->xmpp_username, $user->dxmpp->xmpp_password);
+    	$_SESSION['dxmpp_request_id'] = $conn->rid;
+    	$_SESSION['dxmpp_session_id'] = $conn->sid;
+    	$_SESSION['dxmpp_session_jid'] = $conn->jid;
     	drupal_set_message($_SESSION['dxmpp_request_id']);
-    	 //print_r($dxmpp_http_result);
+    	drupal_set_message($_SESSION['dxmpp_session_id']);
+      drupal_set_message($_SESSION['dxmpp_session_jid']);
+       //print_r($dxmpp_http_result);
 
   }
   else {
-   	$_SESSION['dxmpp_request_id'] = $_SESSION['dxmpp_request_id'] + 1;
-   	drupal_set_message($_SESSION['dxmpp_request_id']);
-  }*/
+    $_SESSION['dxmpp_request_id'] = 0;
+  }
 
   // Add the strophe library.
   drupal_add_js(dxmpp_strophe_path() .'/strophe.js');
@@ -118,8 +115,8 @@ function template_preprocess_dxmpp_chat(&$variables) {
     'stropheConnection' => dxmpp_variable_get('server'),
     'mainUserName' => check_plain($user->name),
     'mainUID' => $user->uid,
-    'jid' => $jid,
-//     'jid' => $_SESSION['dxmpp_session_jid'],
+//    'jid' => $jid,
+    'jid' => $_SESSION['dxmpp_session_jid'],
     'password' => $user->dxmpp->xmpp_password,
     'roster' => dxmpp_roster($user),
     'defaultPicture' => '',
diff --git themes/js/dxmpp.js themes/js/dxmpp.js
index 23786ed..5a5b8f4 100644
--- themes/js/dxmpp.js
+++ themes/js/dxmpp.js
@@ -530,17 +530,30 @@
     // Set our global connection object.
     var _conn = new Strophe.Connection(Drupal.settings.dxmpp.stropheConnection);
 
+    if (Drupal.settings.dxmpp.requestID === 0) {
+      dxmppRequestID = dxmppHandleCookie("dxmpp.requestID");
+/*    //@TODO Add Click again to buttons & user-names 
+      if(dxmppHandleCookie("div-dxmpp") != null) {
+        $('#dxmpp').html(dxmppHandleCookie("div-dxmpp"));
+      }
+*/
+    }
+    else {
+      dxmppRequestID = Drupal.settings.dxmpp.requestID
+    }
     dxmppDebug(Drupal.settings.dxmpp.jid);
     dxmppDebug(Drupal.settings.dxmpp.sessionID);
-    dxmppDebug(Drupal.settings.dxmpp.requestID);
+    dxmppDebug(dxmppRequestID);
 
     // Attach an existing session. @TODO
-//     _conn.attach(Drupal.settings.dxmpp.jid, Drupal.settings.dxmpp.sessionID, Drupal.settings.dxmpp.requestID);
+    _conn.attach(Drupal.settings.dxmpp.jid, Drupal.settings.dxmpp.sessionID, dxmppRequestID, function(status) {
 
     // Connect to the appropriate user on the XMPP server.
-    _conn.connect(Drupal.settings.dxmpp.jid, Drupal.settings.dxmpp.password, function(status) {
-      if (status === Strophe.Status.CONNECTED) {
+//     _conn.connect(Drupal.settings.dxmpp.jid, Drupal.settings.dxmpp.password, function(status) {
+//      if (status === Strophe.Status.CONNECTED) {
+      if (status === Strophe.Status.ATTACHED) {
         // Success!
+        dxmppConnection = _conn;
         $(document).trigger('connected');
       }
       else {
@@ -1066,6 +1079,59 @@
 //     dxmppConnection.send(_send);
 //   }
 
+ dxmppHandleCookie = function(name, value, options) {
+      if (typeof value != 'undefined') { // name and value given, set cookie
+          options = options || {};
+          if (value === null) {
+              value = '';
+              options.expires = -1;
+          }
+          var expires = '';
+          if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
+              var date;
+              if (typeof options.expires == 'number') {
+                  date = new Date();
+                  date.setTime(date.getTime() + (options.expires * 1000));
+              } else {
+                  date = options.expires;
+              }
+              expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
+          }
+          // CAUTION: Needed to parenthesize options.path and options.domain
+          // in the following expressions, otherwise they evaluate to undefined
+          // in the packed version for some reason...
+          var path = options.path ? '; path=' + (options.path) : '';
+          var domain = options.domain ? '; domain=' + (options.domain) : '';
+          var secure = options.secure ? '; secure' : '';
+          document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
+      } else { // only name given, get cookie
+          var cookieValue = null;
+          if (document.cookie && document.cookie != '') {
+              var cookies = document.cookie.split(';');
+              for (var i = 0; i < cookies.length; i++) {
+                  var cookie = jQuery.trim(cookies[i]);
+                  // Does this cookie string begin with the name we want?
+                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
+                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+                      break;
+                  }
+              }
+          }
+          return cookieValue;
+      }
+  };
+
+  $(window).unload(function (){
+    // flush queued messages ???
+    dxmppConnection.flush();
+
+    // save session parameters
+    
+    dxmppHandleCookie("dxmpp.requestID", dxmppConnection.rid, { path: '/', expires: 10});
+    dxmppHandleCookie("div-dxmpp", $('#dxmpp').html(),{ path: '/', expires: 10});
+  });
+
+
 })(jQuery);
 $(document).ready(function() {
 	
