Refactor callback in connect, listen, with test