protected function sendMessages($firstMessageId, $apnURL)
    {
        $errors = array();
        // Loop through all messages starting from the given ID
        $messagesCount = count($this->messages);
        for ($currentMessageId = $firstMessageId; $currentMessageId < $messagesCount; $currentMessageId++) {
            // Send the message
            $result = $this->writeApnStream($apnURL, $this->messages[$currentMessageId]);

            // Check if there is an error result
            if (is_array($result)) {

                // Close the apn stream in case of Shutdown status code.
                if ($result['status'] === self::APNS_SHUTDOWN_CODE) {
                    $this->closeApnStream($apnURL);
                }

                $this->responses[] = $result;
                // Resend all messages that were sent after the failed message
                $this->sendMessages($result['identifier']+1, $apnURL);
                $errors[] = $result;
                if ($this->logger) {
                    $this->logger->error(json_encode($result));
                }
            } else {
                $this->responses[] = true;
            }
        }

        return $errors;
    }