below is mycode though i have set keepalive, socket connection gets closed after sometime.
sb_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,socket.SOL_TCP)
sb_sock.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sb_sock.connect((msg_host, host_port))
reg_msg="REG 0000 "+finder_id+"\r"
sb_sock.send(reg_msg)