public static function send($socket, $command, array $params = array())
    {
        if (!isset(self::$commands[$command])) {
            throw new Exception('Invalid command: ' . $command);
        }

        $data = array();
        foreach (self::$commands[$command][1] as $field) {
            if (isset($params[$field])) {
                $data[] = $params[$field];
            }
        }

        $d = implode("\x00", $data);

        $cmd = "\0REQ" . pack(
            'NN',
            self::$commands[$command][0],
            self::stringLength($d)
        ) . $d;

        $cmdLength = self::stringLength($cmd);
        $written = 0;
        $error = false;
        do {
            $check = @socket_write($socket,
                                   self::subString($cmd, $written, $cmdLength),
                                   $cmdLength);

            if ($check === false) {
                if (socket_last_error($socket) == SOCKET_EAGAIN or
                    socket_last_error($socket) == SOCKET_EWOULDBLOCK or
                    socket_last_error($socket) == SOCKET_EINPROGRESS) {
                    // skip this is okay
                } else {
                    $error = true;
                    break;
                }
            }

            $written += (int) $check;
        } while ($written < $cmdLength);

        if ($error === true) {
            $errno = socket_last_error($socket);
            $errstr = socket_strerror($errno);
            throw new Exception(
                "Could not write command to socket ($errno: $errstr)"
            );
        }
    }