Made the socket variable a global and simplified the code