i want to make a single program for operating both server & client. i want my program to run in such a way that when program is launched, server should start listening for requests from other machines. but when i want to send data to other machines, my server should quit & client is launched so that i start sending data. once complete data has been transferred by client, it should quit & come back to server mode.
Any suggestion on whether its feasible. if yes, then how.

There are a lot of places where servers behave as clients to other servers. So it is very much possible (though i'm not sure about this quitting thing). For example in case of a web search engine, the web server accepts users query and then send crawlers to other servers, thus acting as a client to them. 'how' is not straightforward and you will need to see some implementations and create a solution specific to your problem.