Implement error handling for socket writes, plus other sundry improvements.