/**
* Connect to server.
*
* @param float $timeout Number of seconds until the connect() system call should timeout.
*
* @throws \Exception Exception raised if connection fails.
* @return void
*/
public function connect($timeout = null)
{
if ($timeout === null) {
$timeout = intval(ini_get('default_socket_timeout'));
}
$this->timeout = $timeout;
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
$this->setStreamTimeout($timeout);
$msg = 'CONNECT ' . $this->options;
$this->send($msg);
$connect_response = $this->receive();
if (strpos($connect_response, '-ERR') !== false) {
throw new \Exception("Failing connection: {$connect_response}");
}
$this->ping();
$ping_response = $this->receive();
if ($ping_response !== "PONG") {
if (strpos($ping_response, '-ERR') !== false) {
throw new \Exception("Failing on first ping: {$ping_response}");
}
}
}