You can take up until august of the year you are applying. It will be a late and risky application but it is still an application. I would recommed, the summer before junior year, the winter break of junior year or may/june of the application cycle.
I'd say the summer before you apply, after you've finished as many of your pre-reqs as possible. Use your summer to really study and focus to knock it out of the park. If worse comes to worst, then you have the year to re-take. I'd say the latest you should be aiming to take it is April or May of the year you are actually applying.
I think the summer of application worked best for me because the "all or nothing" feeling helped motivate me to study. Then again, this might come back to haunt me when I get score back. I don't think retaking in August is a viable option for me.