You can apply whenever you want, but most embassies will not approve an application if you're planning to enter the country more than 3 months in the future. It's 3 months. Not 45 days.
You can leave anytime during those 180 days and when you get back you get another 180 days.
A lot of people seem to think that they're stuck in Thailand for the entire duration of their stamp.
And yes, you can get ONE 180 day extension per entry, but the process requires the same documents you used when applying for the visa, so its easier to just fly out and in.