Invoke a function given that the socket is connected