Refactoring ctw.c:

- outputting error codes consistently
- more error handling while reading from socket