/**
  * @param Mirasvit_Helpdesk_Model_Gateway $gateway
  *
  * @return bool
  */
 public function connect($gateway)
 {
     $this->gateway = $gateway;
     $flags = sprintf('/%s', $gateway->getProtocol());
     if ($gateway->getEncryption() == 'ssl') {
         $flags .= '/ssl';
     }
     $flags .= '/novalidate-cert';
     // echo $flags;die;
     $server = new Mirasvit_Ddeboer_Imap_Server($gateway->getHost(), $gateway->getPort(), $flags);
     if (function_exists('imap_timeout')) {
         imap_timeout(1, 20);
     }
     if (!($this->connection = $server->authenticate($gateway->getLogin(), $gateway->getPassword()))) {
         return false;
     }
     $mailboxes = $this->connection->getMailboxNames();
     if (in_array('INBOX', $mailboxes)) {
         $mailboxName = 'INBOX';
     } elseif (in_array('Inbox', $mailboxes)) {
         $mailboxName = 'Inbox';
     } else {
         $mailboxName = $mailboxes[0];
     }
     $this->mailbox = $this->connection->getMailbox($mailboxName);
     return true;
 }
 /**
  * @param Mirasvit_Helpdesk_Model_Gateway $gateway
  *
  * @return string
  */
 public function checkGateway($gateway)
 {
     $result = array();
     $ports = array('gmail.com' => 80, $gateway->getHost() => $gateway->getPort());
     foreach ($ports as $host => $port) {
         $connection = @fsockopen($host, $port);
         if (is_resource($connection)) {
             $result[] = $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.';
             fclose($connection);
         } else {
             $result[] = $host . ':' . $port . ' is closed.';
         }
     }
     return implode('<br>', $result);
 }