I am able to use your sample code without problems. It's likely some local firewall rule is preventing your client from completing its connection to the server. Try running your client and server on the same host, using 'localhost' or '127.0.0.1' in the client connection.
See top answer in Why would a "java.net.ConnectException: Connection timed out" exception occur when URL is up? for more info.
Also, I note you are not setting socket timeouts for connections or reads in your code. Since you are not setting a timeout in your client socket timeout, the default timeout is zero, which is forever, or more likely whatever your OS default socket timeout is. In general, and especially in production code, not setting a socket timeout for connect or read is a bad idea because it will lead to resource consumption problems that will back up your whole system.
Try setting up your client socket with a connection and read timeout, like this:
//use a SocketAddress so you can set connect timeouts
InetSocketAddress sockAddress = new InetSocketAddress("127.0.0.1",40000);
s = new Socket();
//set connect timeout to one minute
s.connect(sockAddress, 60000);
//set read timeout to one minute
s.setSoTimeout(60000);
System.out.println(s);
pw = new PrintWriter(s.getOutputStream());
...