ssh2_exec执行在32kb数据后停止

I am using the php ssh2 methods to establish a connection to a server and transmit a string of ~39Kb. However, after 32Kb the transmission stops with an exception:

ErrorException: ssh2_exec(): Unable to request command execution on remote host in /var/www/xxx/xxxSSH.php:36

Is there an artificial limit on how many bytes can be transmitted in a session and if so, how can I change the limit?

UPDATE

Here's the method producing the error:

public function exec($cmd)
    {
    if (!($stream = ssh2_exec($this->connection, $cmd))) {
        throw new Exception('SSH command failed');
    }

    stream_set_blocking($stream, true);
    $data = "";
    while ($buf = fread($stream, 4096)) {
        $data .= $buf;
    }
    fclose($stream);
    return $data;
}

$cmd contains the string that seems to cause the trouble. What it does is call echo '$extremelyLongString' > /etc/someconfig.cfg