This should probably be a method on server, and implemented in a thread-safe way.

Also, see my comments for the function above. Most of them apply here too.