Looks like you have either a missing DNS or gateway in your setup.
WiFi.gatewayIP() gives you the IP of the gateway, which should be the same as the router's IP in most setups.
You might also split up the client.connect() call into
IPAddress remote_addr;
if (WiFi.hostByName(server, remote_addr)){
if (client.connect(remote_addr, 80)) {
// rest of the code
}
} else {
Serial.println("Couldn't resolve hostname");
}