Improve exception handling in TCP listener