I hesitate to say this because you are using WiFi and there may be something going on I don't understand. But.
I guess cosm doesn't know or care how you send the data, so long as the commands are right.
And
I see the word useragent
And you don't get a connection. So it would appear you need to use the latest com libraries. The code is incomplete, so the libraries you are using is unclear.