better handling of connection errors, provide way to detect terminate command