Shutting down a socket

I’m looking at an issue I’ve seen with the FTP client library when I’m using it over a GPRS link and I was wondering if anyone out there has any suggestions.

What I see happening is that the file is not uploaded completely but the FTP client returns success. I think this is happening due to the slowness of the link and a drop in the GPRS signal.

The client lib writes the last of the data to the data socket, sends the quit and closes the sockets. at some point in the process of flushing the last of the data from the TCP buffers the connection breaks but because the client lib does a very simple shut down with little or no checking, it does not detect the fact that some of the data is not written to the server.

What I would like to do is modify the client library to do a proper shut down. What is the best way to ensure you close a socket, flush ALL the data and cleanly terminate the TCP link or if any of these staps fail that you are aware of the fact?