Hello and sorry you're having problems. I can help you with this as it's a problem I've seen commonly with Samsung sets over the years. The clicking sound you're hearing is the relay on the power supply board cycling due to an out of spec voltage when the set is "cold". Once warmed up, it will operate fine until the next time you shut it off. This is a hardware-related issue inside of the TV- no settings or adjustments will correct this.
There are two possible culprits that cause this problem- either the Power Supply Board, or the Mainboard. They work in tandem to produce the turn on command in the set and the only way to narrow it down is for a shop to measure the standby voltage while the clicking is occurring.
Average cost for this type of repair, unfortunately is in the $275-$300 range- which, is not cost effective on an older 40" LCD set- since you can REPLACE that TV with a brand new entry level 40" LED 4K TV for about $100 more, and have a brand new set with warranty. Additionally parts on these older Samsung LCD TV's are scarce and near impossible to find. Most times only pulled/rebuilt parts are used to repair these sets.
I'm sorry for the bad news here, and realize it is not the answer you'd hoped for, but your money would be better spent towards purchase of a new TV in lieu of repairing this one I'm afraid.
Feel free to reply back if you need more help, or want to reply back with more details. It does not cost you anything more to reply back once you've rated. Please do rate me positively (at least 3 STARS) and I will continue with you until this is sorted. The positive rating ensures I am credited for my work.