remove duplicate socket, add null checks