Refactor to improve implementation of ENABLEAPI command and ForceTwsApiPort option

Quite a sizeable pull request! It was originally a series of about 10 commits, but because of the tortuous path followed in doing this development, I decided that squashing them to a single commit would be clearer, partly because several files were modified in multiple commits, but also the commits themselves didn't always include the right files (finger trouble with NetBeans, but I'm getting there!...), and were therefore rather confusing.

I'd be grateful if someone could build and test this enhancement. I'm fairly confident it works, but the scale of the changes is quite large and I may have missed something.