/**
  * @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);
 }
 protected function fetchEmails()
 {
     $msgs = $errors = 0;
     $max = $this->gateway->getFetchMax();
     $messages = $this->mailbox->getMessages('UNSEEN');
     $emailsNumber = $this->mailbox->count();
     if ($limit = $this->gateway->getFetchLimit()) {
         $start = $emailsNumber - $limit + 1;
         if ($start < 1) {
             $start = 1;
         }
         for ($num = $start; $num <= $emailsNumber; $num++) {
             try {
                 //we can have different errors during fetching of email. we don't want to stop fetching of all queue.
                 $message = $this->mailbox->getMessage($num);
                 if ($this->createEmail($message)) {
                     if ($this->gateway->getIsDeleteEmails()) {
                         $message->delete();
                         $this->mailbox->expunge();
                     }
                     $msgs++;
                 }
                 if ($max && $msgs >= $max) {
                     break;
                 }
             } catch (Exception $e) {
                 Mage::log($e->getMessage(), 'helpdesk_error.log');
             }
         }
     } else {
         foreach ($messages as $message) {
             try {
                 //we can have different errors during fetching of email. we don't want to stop fetching of all queue.
                 if ($this->createEmail($message)) {
                     if ($this->gateway->getIsDeleteEmails()) {
                         $message->delete();
                         $this->mailbox->expunge();
                     }
                     $msgs++;
                 }
                 if ($max && $msgs >= $max) {
                     break;
                 }
             } catch (Exception $e) {
                 Mage::log($e->getMessage(), 'helpdesk_error.log');
             }
         }
     }
 }