had two games that went on for 4years and then in both the game I get a star god/mercenary hideout attack that i cant repel.
Is there some tricks to avoid/counter the end game stuff.
After 4 ingame years you should be plenty ready for mercs, and even star gods. Mercs don't like getting shot by lasers, so if you use mostly piercing weapons and hope that it works against every foe you are out of luck. You have to use different weapons for different enemies.
Both fall to the usual as well: melee. I clear entire merc ships in a turn or two by using a gal in furiosa armor with a blood axe (red codex), while the rest of the gang kills the ones that are flying.
Hovertanks and Sectopods have blue shields that you need to get rid off before you can properly damage them. Try using an arena flak cannon, it can strip the shields in one auto shot. After that something like a portable lascannon (which is a bit hard to use, admittedly) can kill them, but lighter weapons like heavy lasers work as well. If all else fails use weapons that strip armor first, like deathblossoms loaded with chem ammo or chain guns. Sectopods have pretty good reactions though, and usually don't move much. They can kill a gal in annihilator armor in one shot if you are unlucky.
If you have your own hovertanks or XECs with lasers/gauss it is not even fair anymore.
If you are still using RPGs after 4 years you seriously neglected your research.
I am in 2605, but for most of the last 1 1/2 years I've been waiting for star gods with a higher pay grade than novice to capture and go on. I wish they would attack me (at least in my main base, the others might survive as well, but they'd probably have troubles capturing them).
Haven't managed to bring a silver towers down, because my Metallo is too slow. And three other craft (2 Sabres with meteor missiles and laser/gauss and a devastator with 3 meteor and an implosion launcher) were not enough.