It's not trivial - I'd suggest you find some good quality software to use rather than write your own, but if you do write your own, it will need to do better than the ones described here.