Refactor socket message exchange.