Refactor the way listener and handler protocols are defined. Also make sure that once one socket closes, both sides are closed