TITLE: Showing that $\mathbb{R}$ is connected
QUESTION [24 upvotes]: So I know that $\mathbb{R}$ is both open and closed. But given a set, $X\subset \mathbb{R}$, $X\ne \emptyset $ that is both open and closed, how does one show that $X=\mathbb{R}$?  
Here is my attempt: Using the definitions I was given, $X$ open implies that for every $x\in X$ there is a $\delta \gt 0$, $(x-\delta,~ x+\delta)\subset X$.    Also, since $X$ is closed , $B=\mathbb{R}$\  $X$ is open.  
However, I don't know how to proceed.
NB: This is not a homework problem.

REPLY [9 votes]: Theorem. A compact interval $[a,b]$ is connected.
Suppose $[a,b]$ were disconnected. Then there would exist a continuous surjective function $f$ from $[a,b]$ onto the discrete space $\{0,1\}$. Said discrete space can also be considered as a subset of $\mathbb R$ with the subspace topology, so $f$ would be continuous as a function from $[a,b]$ to $\mathbb R$ as well. But by the intermediate value theorem, it would attain every value in the interval $[0,1]$ contradicting that it's a function onto $\{0,1\}$. Hence such a function cannot exist, and $[a,b]$ must be connected. $\square$
Theorem. If a space $X$ contains at least two points and any pair of distinct points of $X$ are contained in a connected subset, then $X$ is connected.
Proof. Suppose we have disjoint nonempty open sets $A,B \subset X$ with $A \cup B = X$, i.e. a disconnection of $X$. By the assumption there are $a \in A$ and $b \in B$ contained in a connected $S \subseteq X$. $S$ has the subspace topology, so $A \cap S$ and $B \cap S$ would then be disjoint nonempty open subsets of $S$ with $(A \cap S) \cup (B \cap S) = (A \cup B) \cap S = X \cap S = S$, yielding a disconnection of $S$, which is our desired contradiction. $\square$
Corollary. $\mathbb{R}$ is connected.