Please don't post the same question in multiple forums. We all read several forums so you are not increasing your audience. You also may be wasting the communities time as we answer the same thing in multiple places.
If you are using J2SE 5.0 then you can call the method setConnectTimeout. Otherwise you will have to start a new Thread which keeps track of how much time has elapsed since you tried to open the connection.