Improve socket management