public function write($buffer, $addNewLine = true) {

        if (!$this->socket) {
            throw new \Exception("Telnet connection closed");
        }

        // clear buffer from last command
        $this->clearBuffer();

        if ($addNewLine == true) {
            $buffer .= "\n";
        }

        $this->global_buffer .= $buffer;
        if ($this->debug_mode === 2 || $this->debug_mode === 3) {
            $this->log($buffer);
        }
        if (!fwrite($this->socket, $buffer) < 0) {
            throw new \Exception("Error writing to socket");
        }

        return true;
    }