/**
  * Writes $contents to Storage
  * 
  * @param mixed $contents
  * @throws Zend_Auth_Storage_Exception If writing $contents is not completed
  * @return bool
  */
 public function write($contents)
 {
     $requestObject = new Zend_Controller_Request_Http();
     if ($cookie = $requestObject->getCookie(self::$_cookieName, FALSE)) {
         //Decrypt Cookie
         $encryption = new Cryptography_EncryptionService('1111834');
         $decrypted = $encryption->decrypt($cookie);
         //Separate Session ID from UserID
         $sessioncookie = explode('||', $decrypted);
         $sessionid = $sessioncookie[0];
         //Check Session Table
         try {
             $session = Doctrine_Core::getTable('Model_Session')->findOneBy(self::$_sessionidfield, $sessionid);
         } catch (Doctrine_Exception $e) {
             throw new Zend_Auth_Storage_Exception();
         }
         if (!$session) {
             $session = new Model_Session();
             $session->{self::$_accessedfield} = time();
             $session->{self::$_useridfield} = $contents->id;
             $session->{self::$_hostnamefield} = $_SERVER['REMOTE_ADDR'];
             $session->{self::$_datafield} = serialize($contents);
             try {
                 $session->save();
             } catch (Doctrine_Exception $e) {
                 throw new Zend_Auth_Storage_Exception();
             }
             $encryption = new Cryptography_EncryptionService('1111834');
             $hashing = new Cryptography_HashingService();
             $cookievalue = $session->id . '||' . $hashing->Compute($session->{self::$_hostnamefield});
             if (setcookie(self::$_cookieName, $encryption->encrypt($cookievalue), 0, '/')) {
                 return true;
             } else {
                 throw new Zend_Auth_Storage_Exception();
             }
         }
         $session->{self::$_accessedfield} = time();
         $session->{self::$_datafield} = serialize($contents->toArray());
         try {
             $session->save();
         } catch (Doctrine_Exception $e) {
             throw new Zend_Auth_Storage_Exception();
         }
         return true;
     } else {
         $session = new Model_Session();
         $session->{self::$_accessedfield} = time();
         $session->{self::$_useridfield} = $contents->id;
         $session->{self::$_hostnamefield} = $_SERVER['REMOTE_ADDR'];
         $session->{self::$_datafield} = serialize($contents);
         try {
             $session->save();
         } catch (Doctrine_Exception $e) {
             throw new Zend_Auth_Storage_Exception();
         }
         $encryption = new Cryptography_EncryptionService('1111834');
         $hashing = new Cryptography_HashingService();
         $cookievalue = $session->id . '||' . $hashing->Compute($session->{self::$_hostnamefield});
         if (setcookie(self::$_cookieName, $encryption->encrypt($cookievalue), 0, '/')) {
             return true;
         } else {
             throw new Zend_Auth_Storage_Exception();
         }
     }
 }