the socket has a tendency to be closed when we get into an error condition - bracket the write/end in a try catch.