Fixed error handling bugs

Allowed errors to be sent after Write has been called, since an error
could occur during the response.

Also fixed another bug where data might be sent after an error has been
sent, where the buffer is flushed.