Updated code with sockets