I didn't see the typo. But this isn't what I meant.
You created the socket on the stack, which means it gets destroyed at the end of the function. Since sockets are asynchronous, the data might not have been sent already at the moment the socket object is destroyed.