crashes: refactor code, add asserts
crashes: make send fail on client disconnects

Writer.flushed w will not become determined if there is a write error,
which will cause the controller to hang unless every send is checked to
make sure the client is still alive first.