You can try to write your 'function' as an object method, with proper encapsulation.
Then either:
Using fork() to do that is probably a very bad idea, unless you really know what you do and have absolutely no other solution.
If you really have socket connections, and the probe touches them, you can't guarantee no side-effect (unless all your socket accesses are idempotent), so it is likely unfeasible.
Sorry I can't be really more precise as your question is quite vague...