public static function connect($host = 'localhost', $timeout = 2000)
    {
        if (!count(self::$magic)) {
            foreach (self::$commands as $cmd => $i) {
                self::$magic[$i[0]] = array($cmd, $i[1]);
            }
        }

        $err = '';
        $errno = 0;
        $port = self::DEFAULT_PORT;

        if (strpos($host, ':')) {
            list($host, $port) = explode(':', $host);
        }

        $start = microtime(true);
        do {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            $socket_connected = @socket_connect($socket, $host, $port);
            if ($socket_connected) {
                socket_set_nonblock($socket);
                socket_set_option($socket, SOL_TCP, 1, 1);
            }
            $timeLeft = ((microtime(true) - $start) * 1000);
        } while (!$socket_connected && $timeLeft < $timeout);

        if (!$socket_connected) {
            $errno = socket_last_error($socket);
            $errstr = socket_strerror($errno);
            throw new CouldNotConnectException(
                "Can't connect to server ($errno: $errstr)"
            );
        }

        self::$waiting[(int) $socket] = array();

        return $socket;
    }
public static function connect($host = 'localhost', $timeout = 2000)
    {
        if (!count(self::$magic)) {
            foreach (self::$commands as $cmd => $i) {
                self::$magic[$i[0]] = array($cmd, $i[1]);
            }
        }

        $err = '';
        $errno = 0;
        $port = self::DEFAULT_PORT;

        if (strpos($host, ':')) {
            list($host, $port) = explode(':', $host);
        }

        $start = microtime(true);
        do {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            $socket_connected = @socket_connect($socket, $host, $port);
            if ($socket_connected) {
                socket_set_nonblock($socket);
                socket_set_option($socket, SOL_TCP, 1, 1);
            }
            $timeLeft = ((microtime(true) - $start) * 1000);
        } while (!$socket_connected && $timeLeft < $timeout);

        if (!$socket_connected) {
            $errno = socket_last_error($socket);
            $errstr = socket_strerror($errno);
            throw new CouldNotConnectException(
                "Can't connect to server ($errno: $errstr)"
            );
        }

        self::$waiting[(int) $socket] = array();

        return $socket;
    }
public static function connect ( $ host = 'localhost' , $ timeout = 2000 ) { if ( ! count ( self :: $ magic ) ) { foreach ( self :: $ commands as $ cmd => $ i ) { self :: $ magic [ $ i [ 0 ] ] = array ( $ cmd , $ i [ 1 ] ) ; } } $ err = '' ; $ errno = 0 ; $ port = self :: DEFAULT_PORT ; if ( strpos ( $ host , ':' ) ) { list ( $ host , $ port ) = explode ( ':' , $ host ) ; } $ start = microtime ( true ) ; do { $ socket = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP ) ; $ socket_connected = @ socket_connect ( $ socket , $ host , $ port ) ; if ( $ socket_connected ) { socket_set_nonblock ( $ socket ) ; socket_set_option ( $ socket , SOL_TCP , 1 , 1 ) ; } $ timeLeft = ( ( microtime ( true ) - $ start ) * 1000 ) ; } while ( ! $ socket_connected && $ timeLeft < $ timeout ) ; if ( ! $ socket_connected ) { $ errno = socket_last_error ( $ socket ) ; $ errstr = socket_strerror ( $ errno ) ; throw new CouldNotConnectException ( "Can't connect to server ($errno: $errstr)" ) ; } self :: $ waiting [ ( int ) $ socket ] = array ( ) ; return $ socket ; }
Connect to Gearman.

Opens the socket to the Gearman Job server. It throws an exception if
a socket error occurs. Also populates MHlavac\Gearman\Connection::$magic.

@param string $host    e.g. 127.0.0.1 or 127.0.0.1:7003
@param int    $timeout Timeout in milliseconds

@throws \MHlavac\Gearman\Exception when it can't connect to server

@return resource A connection to a Gearman server

@see MHlavac\Gearman\Connection::$waiting
@see MHlavac\Gearman\Connection::$magic
@see MHlavac\Gearman\Connection::$commands
Connect to Gearman .