How to prove that a topological space is connected iff it has exactly two clopen subsets?

$(\Leftarrow)$: Considering the contrapositive... Let $X$ be a disconnected topological space. That is to say, there exist non-empty open subsets $U$, $V \subset X$ such that $U \cap V = \emptyset $ and $U \cup V=X$. Here $U$ and $V$ are clopen subsets of $X$ since $U^c=V$ and $V^c=U$.

$(\Rightarrow)$: Suppose now that $X$ is connected. Then there exist two clopen subsets of $X$, namely $\emptyset$ and $X$ itself. Here, recall that $\emptyset$ and $X$ are clopen by definition.


Always there are at least two clopen subsets, namely $\emptyset,X$. If there is another one, let's say $U$, then $U\neq\emptyset$ and $U\neq X$.
Consider the subsets $U$ and $U^c$ to show that $X$ is disconnected.


We would like to prove that $X$ is connected iff the only sets in $X$ that are clopen are $\varnothing$ and $X$.

$\Rightarrow$: (by contrapositive). Assume that there is a clopen subset $A$ of $X$ such that $A\neq \varnothing$ and $A\neq X$. Then the pair $(A, A^c)$ forms a separation of $X$. Thus, $X$ is not connected.

$\Leftarrow$: (by contrapositive). Suppose $X$ is not connected. Then there exist two nonempty disjoint open sets $A$ and $B$ such that $X=A\cup B$. This implies that $A$ and $B$ are complement to each other. Thus, $A=B^c$. Hence, $A$ is closed. Therefore, $A$ is a clopen subset of $X$ satisfying $A\neq \varnothing$ and $A\neq X$.