public function run()
    {
        pcntl_signal_dispatch();
        if ($this->service == null || !$this->service->isAlive()) {
            throw new \RuntimeException('Service not running.');
        }

        $workerHandlerSocket = $this->socketFactory->createRequest(Factory::MODE_CONNECT, $this->workerHandlerAddress);
        $serviceSocket = $this->socketFactory->createRequest(Factory::MODE_BIND, $this->serviceAddress);

        $this->worker = new Worker();
        $this->worker->setLogger($this->getLogger());

        $comm = new WorkerCommunication($this->worker, $workerHandlerSocket, $serviceSocket);
        $comm->setLogger($this->getLogger());
        $comm->setDelay($this->delay);

        $this->registerSignalHandler();
        $this->running = true;

        $comm->start();
        while (true) {
            $comm->process();
            pcntl_signal_dispatch();

            if (!$this->service->isAlive()) {
                $this->worker->onServiceDown();
            }

            if ($this->worker->getState() == Worker::INVALID) {
                $this->stop();
                break;
            }
        }

        // Kill the service.
        $this->service->killAndWait();
        $this->worker->onServiceDown();

        $this->getLogger()->debug('Run completed with state: '.$this->worker->getState());
    }