Refactor message processing for socket