4. Solution: Note that $\sin x \neq 0, \cos x \neq 0$, and multiply both sides of the equation by $\operatorname{tg} x$. We get $\sqrt{2+\cos 2 x+\sqrt{3} \operatorname{tg} x}=\sin x+\sqrt{3} \cos x$. Under the condition $\sin x+\sqrt{3} \cos x \geq 0$, both sides of this equation can be squared. Since $\sin x+\sqrt{3} \cos x=2 \sin \left(x+\frac{\pi}{3}\right)$, the inequality holds if $-\frac{\pi}{3}+2 \pi n \leq x \leq \frac{2 \pi}{3}+2 \pi n, n \in Z$. Under the found restrictions and conditions $\sin x \neq 0, \cos x \neq 0$, the equation is equivalent to the following:

$2+\cos 2 x+\sqrt{3} \operatorname{tg} x=\sin ^{2} x+2 \sqrt{3} \sin x \cos x+3 \cos ^{2} x, \operatorname{tg} x-2 \sin x \cos x=0, \frac{\sin x}{\cos x}-2 \sin x \cos x=0$, $\sin x\left(\frac{1}{\cos x}-2 \cos x\right)=0$. Thus, we arrive at the equation: $\cos ^{2} x=\frac{1}{2}, \cos x= \pm \frac{\sqrt{2}}{2}, x= \pm \frac{\pi}{4}+\pi n, n \in Z$. Considering the restrictions, we obtain the solutions to the original equation: $x= \pm \frac{\pi}{4}+2 \pi n, \quad n \in Z$.
Answer: $x= \pm \frac{\pi}{4}+2 \pi n, \quad n \in Z$.