Use closures for connect and read callbacks
Don't close stream immediately if write fails, also log failure
Handle io stream errors gracefully