fix use of tcp sockets and misc cleanup