Working socket code with single connection and error outputs