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;
    }