php raw ssh2_connection不返回值

What I want do

I want to create a ssh connection to a teamspeak server. With that raw connection it should be possible to put and get commands from the console.

What I´ve

        $socket = @ssh2_connect($this->runtime['host'], $this->runtime['queryport']);

        if($socket === false)
        {
            $this->addDebugLog('Error: connection failed!');
            return $this->generateOutput(false, array('Error: connection failed!'), false);
        }
        else
        {
            if(@ssh2_auth_password($socket, $this->escapeText($username), $this->escapeText($password)))
            {
                if(($shell = @ssh2_shell($socket, "raw")) === false)
                {
                    return $this->generateOutput(false, array('Error: failed to open a secure shell on server!'), false);
                }
                else
                {
                    $this->runtime['ssh'] = $socket;
                    $this->runtime['socket'] = $shell;
                    @stream_set_timeout($this->runtime['socket'], $this->runtime['timeout']);
                    @stream_set_blocking($this->runtime['socket'], true);
                    return $this->generateOutput(true, array(), true);
                }
            }
            else
            {
                return $this->generateOutput(false, array('Error: invalid loginname or password!'), false);
            }
        }

What´s wrong

Cause of @stream_set_blocking($this->runtime['socket'], true); this line my script is running into a timeout. Also I´ve got some the problem that fgets always return a empty result. Did I did something wrong?

Problem

I got here a timeout cause the stream waiting until it will be closed.

Solution

If I close the stream after my read and write stuff I got my answer.

                    @stream_set_timeout($this->stream, intval($this->runtime['timeout']));
                    @stream_set_blocking($this->stream, true);
                    echo fgets($this->stream);
                    echo fgets($this->stream);
                    fwrite($this->stream, 'quit' . "
");
                    $this->runtime['socket'] = $socket;
                    return $this->generateOutput(true, array(), true);