How to implement a function to make a software update using my own application.

How can I implement a function to make a software update using my own application.
The system (running in RCM3365) is connected via TCP/IP to a host, the host controls the system sending commandos to it. I wish to develop a command in the system that will receive an image of the new code end then change the actual program with the image. The part I don’t know how to do is the parte where the old existing software is replaced by the new one. I suppose that a piece of code must be installed in RAM and this code is the one that make the replacement after the image is received and then return control to the new software rebooting the RCM.