Many systems provide the ability to keep tcp connections alive, so they aren't reset by peers or by routers because of inactivity.
Even if this feature is present, only a few programs correctly implement the code to use it. If your favourite foo-client is not written with support for tcp keepalive, you'll continue to see your connection reset.
libkeepalive library provides a way to enable tcp keepalive support in any program that uses shared libraries (e.g.: glibc shared object) to perform network operations. Using the preload method, you will be able to intercept normal program execution and to inject the code needed to enable the keepalive routines, everything done without modifying the original binary file and with no need to gain root privileges.
Latest version of libkeepalive is available on http://libkeepalive.sourceforge.net/#download.
Just run make (use gmake if your default make is another one).
Simply copy libkeepalive.so in some cool directory (/usr/lib, or ~/lib if you're not so elite to have root privileges).
If you want to enable keepalive in suid programs too, you have to copy libkeepalive.so in /lib or /usr/lib and suid it (according to ld.so manpage). In any other scenarios the library will not work with suid binaries.
libkeepalive will do its job if you set the environment variable LD_PRELOAD to the path of libkeepalive.so shared library before executing the target program.
You must specify the full path of the library if ld.so cannot is not able to find it. You can also set the variable in your startup script (i.e.: ~/.profile), depending from your shell.
For informations about setting environment variables, refer to your shell documentation.
Host administrators may want to impose the use of libkeepalive to all their users and all daemons loaded by the system startup process.
Adding the full path of the libkeepalive.so dynamic library to /etc/ld.so.preload you obtain the same result as the LD_PRELOAD variable is always set, forcing the use indipendently from the environment.
Disabling for a specific execution
If you need to disable the action of libkeepalive for a single job, or if your admin set the ld.so.preload, you can set the KEEPALIVE environment variable to "off", inhibiting the library even if it will be loaded.
DON'T FORGET to configure your system-specific parameters for keepalive! Many times, defaults are not what you need and you have to change your settings (see your system documentation for this).