I know that some OSes provide a per socket timeout that I can set in my application code (instead of a system-wide timeout that can only be set by root). But that does not seem like a very portable solution.
SO_SENDTIMEO is your portable friend (can't remember whether it's exposed by the network package, though).