Problem in a step in the proof that a continuous function sends connected subsets into connected subsets. 
Let $f: S\rightarrow M$ be  a function from a metric space $S$ to another metric space  $M$. Let $X$ a connected subset of $S$. If $f$ is continuous on $X$ then $f(X)$ i a connected subset of $M$.

Proof:
Suppose that $f$ is continuous and $f(X)$ is not connected, then exist $A,B$ disjoint nonempty open sets in $f(X)$ such that $f(X)=A \cup B$.
If I consider the sets:
$S_1 = X \cap f^{-1}(A)$,
$S_2 = X \cap f^{-1}(B)$
How can I prove that $S_1, S_2$ are open?
I know that $f^{-1}(A), f^{-1}(B)$ are open but I don't know if $X$ are open.
 A: You can not, and you need not, prove that $S_1,S_2$ are open. A set $X$ is not connected if and only if there exist disjoint open sets $U$ and $V$ which together cover $X,$ i.e., with $X \subset V\cup W.$
In your case, the sets $f^{-1}(A)$ and $f^{-1}(B)$ play the role of $U$ and $V.$ Also, you can only assume $f(X) \subset A \cup B,$ not $f(X) = A \cup B.$
The important point here is that you have to distinguish between the concept of a connected topological space, and a connected subset of a topological space (see 'Connected space' on Wikipedia), where the condition $X=U\cup V$ appears with $U$ and $V$ being open in the subspace topology. A set $U$ is open in the subspace topology of $X$ if there a set $U'$ open in $S$ such that $U=U'\cap X.$
Here an example: consider the real line $S=\mathbb{R},$ and the subset $X=\{0,3\}.$ Clearly, $X$ should be disconnected in any reasonable definition, but $X$ is never the union of two open (open in $\mathbb{R}$!) sets, as it would have to be open itself. However, $X=\{0\} \cup \{2\},$ and $\{0\}$ and $\{2\}$ are not open in $\mathbb{R},$ but they both are open in the subspace topology on $X,$ as $\{0\} = X \cap (-1,1)$ and $\{2\} = X \cap (1,3),$ and the intervals $(-1,1)$ and $(1,3)$ are open in $\mathbb{R}.$
