regarding tcp_keepalive

I’m trying to use tcp_keepalive option to make my server not hang in case
client is dead…
I set the timeout to 1 sec, it doesnt seem to make any difference…stil
hangs if my client
lost power or connection cable is loose. what did I do wrong? Help
using getprotobyname(), should I use that function?

on = 1;
setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on));

setsockopt (sock, IPPROTO_TCP, TCP_KEEPALIVE, (char *)&on, sizeof(on));