It seems to me that you’ve formed a habit or an addiction to a particular course of behaviour. To that end you might be said to now be lacking the full consent in the actions since it is the addiction controlling you rather than your will.
May I suggest a few practical means by which you can attempt to beat the habit?
Not least would be the recognition that you actually have an addiction and take that to God. Self awareness is one of the tools which we can use to help heal ourselves.
You mention ‘sins of impurity’. I think most of us will know what you’re talking about (and those of us who seem not to are probably embarrassed to admit it!).
One of the ways of breaking a habit is to remove the enablers for it in our lives. Have you found that a substantial change in your routine makes you less prone to falling? To that end, change your lifestyle. Change your habits at home.
If you are using impure materials, rather than just your imagination, then get rid of them. If you are using the internet, install blocking software or have your ISP put automatic ‘parental controls’ on your account (if they do it). If your computer is the route to impure materials and you live in a family environment, if the computer is in a private area: move it somewhere public where you wouldn’t want others to see what’s on the screen. If you live alone, move it out of your bedroom. Never let it in there again. Make that area a holy space, designed for being at one with God (and your wife, if you’re married). Cultivate the habit of commending yourself to the care of the saints and the angels at bedtime. Place a crucifix at the FOOT of your bed, where you can see it (many people will traditionally have it on the wall behind them, and out of sight can sometimes mean out of mind).
Listen to calming music before bed. Set aside a specific time for it every night. Maybe even go to sleep with it on in the background. Try something gentle. I find Early Music from groups like Anuna (an Irish choral group first heard on Riverdance) very restful and, importantly, many songs are religious in origin. You will know which music works best for you.
Above all, rely on the mercy of God. Please don’t fear Him just because you are in the midst of your weakness. He just wants to help.
Just because you are nearly 40 (hey, we have something in common!) means nothing. Nobody is too old to change. Nobody is too old to ask for help. Nobody is too old to receive the Grace of God. Nobody is so old that should either feel abandoned or abandon themselves. And 40 isn’t even remotely old.
You will be free of this.