/**
  * Executes the broker
  *
  * @throws \Exception
  */
 public function execute()
 {
     // Ensure that the socket connections have been made.
     if (!$this->clientChannel || !$this->workerChannel) {
         throw new \Exception("Please run 'connect' before executing the broker!");
     }
     // Some initialization
     $read = array();
     $write = array();
     $heartbeatAt = microtime(true) + $this->config->getInterval();
     // Main loop
     while (true) {
         $poll = new ZMQPoll();
         $poll->add($this->workerChannel, ZMQ::POLL_IN);
         // Poll the client channel only if we have available workers
         if ($this->queue->size()) {
             $poll->add($this->clientChannel, ZMQ::POLL_IN);
         }
         // Get all the events and if any events occurred process them.
         $events = $poll->poll($read, $write, $this->config->getInterval() * 1000);
         if ($events > 0) {
             foreach ($read as $socket) {
                 $message = new MultipartMessage($socket);
                 $message->recv();
                 //  Handle worker activity on backend
                 if ($socket === $this->workerChannel) {
                     $identity = $message->unwrap();
                     $this->handleMessage($message, $identity);
                 } else {
                     //  Now get next client request, route to next worker
                     $identity = $this->queue->getWorker();
                     $message->wrap($identity);
                     $message->setSocket($this->workerChannel)->send();
                 }
             }
             $heartbeatAt = $this->validateQueue($heartbeatAt);
         }
     }
 }